Home >Backend Development >PHP Tutorial >The difference between include and require in PHP

The difference between include and require in PHP

WBOY
WBOYOriginal
2016-07-30 13:31:29913browse

The difference between include and require. In fact, there is not much difference between the two. If the file to be included does not exist, include prompts notice, and then continues to execute the following statement. require prompts a fatal error and exits.

According to the test, under the win32 platform, they are included first and then executed, so it is best not to have include or require statements in the included files, which will cause directory confusion.

Maybe the situation is different under *nux, but I haven’t tested it yet. If you don’t want a file to be included multiple times, you can use include_once or require_once## to read and write document data:

<span> 1</span><span>function</span> r(<span>$file_name</span><span>) {
</span><span> 2</span><span>$filenum</span>=@<span>fopen</span>(<span>$file_name</span>,"r"<span>);
</span><span> 3</span> @<span>flock</span>(<span>$filenum</span>,<span>LOCK_SH);
</span><span> 4</span><span>$file_data</span>=@<span>fread</span>(<span>$filenum</span>,<span>filesize</span>(<span>$file_name</span><span>));
</span><span> 5</span> @<span>fclose</span>(<span>$filenum</span><span>);
</span><span> 6</span><span>return</span><span>$file_data</span><span>;
</span><span> 7</span><span>}
</span><span> 8</span><span>function</span> w(<span>$file_name</span>,<span>$data</span>,<span>$method</span>="w"<span>){
</span><span> 9</span><span>$filenum</span>=@<span>fopen</span>(<span>$file_name</span>,<span>$method</span><span>);
</span><span>10</span><span>flock</span>(<span>$filenum</span>,<span>LOCK_EX);
</span><span>11</span><span>$file_data</span>=<span>fwrite</span>(<span>$filenum</span>,<span>$data</span><span>);
</span><span>12</span><span>fclose</span>(<span>$filenum</span><span>);
</span><span>13</span><span>return</span><span>$file_data</span><span>;
</span><span>14</span> }


The above has introduced the difference between include and require in PHP, including the relevant aspects. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn