Search code examples
phpincluderequire-once

Some contents of an included file is not loading


I am trying to load contents of a file (myfile.php) into another (index.php) using the include_once() php api. But it is not loading the contents.

// contents inside index page
if(include_once('myfile.php')){
    echo D; // this is working
    echo $b; // this is not working
} else {
    echo "unable to include the file";
}   

// contents inside myfile.php
define('D', '123');
$b = "abcd";

Solution

  • You might be not including it correctly.

    You can find this in the documentation:

    <?php
    // won't work, evaluated as include(('vars.php') == 'OK'), i.e. include('')
    if (include('vars.php') == 'OK') {
        echo 'OK';
    }
    
    // works
    if ((include 'vars.php') == 'OK') {
        echo 'OK';
    }
    ?>