I had a file test.php with the following code
<?php
//name of this file is test.php
$foo = "some value";
include_once 'extras.php';
$bar = "another value";
echo "test.php: foo is $foo and bar is $bar <br />";
?>
Another file with the code
<?php
echo "extras.php: foo is $foo and bar is $bar <br />";
?>
The output indicates that echo statement from extras.php is included as well in test.php, whereas I being a newbie was assuming that include_once or include constructs only deal with the variables and functions and disregard other stuff. What is correct, guide please.
Thank you
include_onceincludes the entirety of the script into the calling script, so long as it hasn’t been previously included. This includes all variables, functions,echos…everything. In effect, the code is imported into the space where theinclude_onceline is, so it is possible to reference variables of anincludeded script in the calling script.The script you posted looks like this, after the
includes are processed:Just be aware that if the file isn’t found, the script doesn’t error out, but variables set up in the
included script won’t be set. For that, userequire_once.