6

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 ?"

1

5 Answers 5

4

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.

Sign up to request clarification or add additional context in comments.

1 Comment

You're right. That's why I can't understand why the "KO" is displayed.
3

This only works the way you expect if menu_js.php contains return 'OK';. Otherwise the require works just fine, but the returned value is not "OK", which is why your "KO" condition is triggered. require doesn't return 'OK'. If the require does not work, the program is halted immediately, your false condition will never be hit the way you think it will.

Comments

2

There are some mistakes here:

  1. According to the PHP documentation:

    require is identical to include except upon failure it will also produce a fatal E_COMPILE_ERROR level error.

    A fatal error will stop your script execution.

  2. require returns what the included file returns:

    // a.php
    return 'test';
    
    // b.php
    $result = require 'a.php';
    echo $result; // will display 'test'
    

So, don't test the return value of require! Use is_file(), then if true, require that file!

Comments

1

You cannot use require in if

Use this

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

3 Comments

Thanks for answer but, as I said in the question, the EXACTLY SAME line (eg the "require" one) works in many other pages of my site so i KNOW for sure that the file exist.
@jhonraymos: From the PHP include manual: Successful includes, unless overridden by the included file, return 1. It is possible to execute a return statement inside an included file in order to terminate processing in that file and return to the script which called it.
Your first sentence is my answer : You cannot use require in if But where is it written in documentation ?
1

While it's true that the require will not return anything, making the conditional always return 'KO' since the left hand value in the parenthesis will never equal 'OK', you can check to see if the file has failed by setting the display_errors ini value in this script temporarily On.

Since you have your error reporting set to catch this, your display_errors is probably off, therefore not showing you this. Set it for this script like this:

ini_set('display_errors', 'on');

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.