Q: What does include_once includes? Variables/functions only or other stuff as well? 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

A: include_once includes 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 the include_once line is, so it is possible to reference variables of an includeded script in the calling script.
The script you posted looks like this, after the includes are processed:
<?php
//name of this file is test.php
$foo = "some value";
echo "extras.php: foo is $foo and bar is $bar <br />";
$bar = "another value";
echo "test.php: foo is $foo and bar is $bar <br />";
?>

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, use require_once.

A: You can think of include_once as "if the file has not already been included, remove this line and paste the contents of the file here".
There is one to me somewhat strange exception to thinking like this related to variable scope of variables defined in the included file (they will end up in the global scope). More about that here.

A: You better understand include and include_once
For example, we may take 3 files;
FUNCTIONS.PHP
<?php
function foo(){
echo 'some code';
}
?>

GLOBALS.PHP
<?php
include('FUNCTIONS.PHP');
foo();
?>

HEADER.PHP
<?php
include('FUNCTIONS.PHP');
include('GLOBALS.PHP');
foo();
?>

Now if you try to open HEADER.PHP you will get an error because GLOBALS.PHP includes FUNCTIONS.PHP already. You will get an error saying that function foo() was already declared in GLOBALS.PHP, and I also included in HEADER.PHP - which means I have included FUNCTIONS.PHP two times.
So to be sure I only include FUNCTIONS.PHP only ONE time, I should use the include_once() function, so my HEADER.PHP should look like this:
<?php
include_once('FUNCTIONS.PHP');
include('GLOBALS.PHP');
?>

Now when I open HEADER.PHP, I will not get an error anymore because PHP knows to include the file FUNCTIONS.PHP only ONCE.
So to avoid getting an error, it would just be safe to use include_once() instead of include() function in your php code.
If you know you are messy with your PHP code, then its safer to use the include_once(), but if you keep track of all your code, then its ok to use the include() function.

A: You should read through include and include_once function reference in the PHP manual at first. Short story long - both statement basically merge codes in the original / included files, so everything in the included file is run / available in the source file, be it functions, variables, classes, echo statements whatsoever... (however, include_once as the name already suggests includes the file only once, the first time, if it has not been already included, regardless of how many include_once statements you put in there)...
