The require () and include () operators act differently, but in a conditional expression?

I wanted to see if there was any reason other than necessary or demanding (hence the name). I came across this statement:

In contrast include(), require()it will always read in the target file, even if its line is never executed. If you want to conditionally specify a file, use include(). The conditional statement will not affect require(). However, if the line on which it occurs require()is not executed, none of the code in the target file will be executed.

The way I interpret this is that it includes inside conditional expressions that do not end with a run, it will not actually include it there, which for me seems less expensive for the server. I'm just wondering if this is true. I did not see this in the manual, so I am skeptical.

+3
source share
2 answers

If you include inside a false statement, it will not parse. The same is true for require. The main and only difference is that require will check if the file exists, even if it has never been executed. Well, that was my experience with him, at least.

So, both are executed only after execution, but require will show an error if the file does not exist, regardless of whether it was executed.

+4

, , , :

function require($file) {
   if (!is_readable($file)) {
      die("can't read $file");
   }
   include($file);
}

, PHP, . _once , , .

+1

All Articles