All the code in the included file is "copy pasted" into the place where it says "include file.ext". But normal rules apply, so code inside a function will not be run until you call the function.
file1.php
echo '1';
include 'file2.php';
echo '1';
file2_func();
echo $file2_var;
echo $file2_func_var; // error
file2.php
$file2_var = 'b'; // scope of this var is still global
echo '2';
function file2_func() {
$file2_func_var = 'func_b'; // scope of this var is still local to function
echo '2_func';
}
Output:
1
2
1
2_func
b
Error...
And file1.php is equal to
echo '1';
// this is where it said to include file2.php. copy pasting contents from that file here
$file2_var = 'b'; // scope of this var is still global
echo '2';
function file2_func() {
$file2_func_var = 'func_b'; // scope of this var is still local to function
echo '2_func';
}
// end of the included file
echo '1';
file2_func();
echo $file2_var;
echo $file2_func_var; // error
The ONLY thing that differs from included/required content to non-included content is that you can use return statements (pretty much as if you were inside a function). But, if you need to do this, you should probably restructure your code instead.
filea.php
if ((include 'fileb.php') == 2)
echo 'fileb.php returned 2';
fileb.php
if ($something)
return 1;
else
return 2;