Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP require fails with no error

Here is the code I have been struggling for several hours:

if ((require $_SESSION['ROOT_PATH'] . '/templates/core/menu_js.php') == 'OK') {
   echo 'OK';
} else {
   echo 'KO';
}

If I understand the PHP documentation on the "require" directive correctly, the "KO" should never be written because, if the require doesn't work, an error is raised.

In my case, the "KO" is always displayed even with error tunning :

error_reporting(E_ERROR | E_WARNING | E_PARSE | E_COMPILE_ERROR);

Note that the same require (or include) works perfectly in other pages of the site.

EDIT

The if structure has been added after watching the documentation. At first, I had a single line :

require $_SESSION['ROOT_PATH'] . '/templates/core/menu_js.php';

As I checked that this line was not working, I added the if.

By the way. Required page (when it works) adds a script tag to calling page that I never see on this unique page. On any other page where this require is used, the script appears on output.

So my question should be "if the output of the required php file is not displayed, why is there no error raised ?"

like image 226
Targol Avatar asked Oct 18 '12 13:10

Targol


1 Answers

According to the documentation

Successful includes, unless overridden by the included file, return 1... Also, it's possible to return values from included files. You can take the value of the include call as you would for a normal function.

So your file isn't returning 'OK'. It's returning either 1 (for success) or a custom value.

require is a language construct, not a standard function. By using require you're already indicating the code should fail if the file isn't found. Most likely you do not need to check the return value.

like image 77
Matt S Avatar answered Sep 20 '22 19:09

Matt S