I have two files X.php and Y.php as shown below and I am not quite able to figure out what the include statement signifies. In both, these cases the code did not do what I expected it to do. So, I would like to know how it works.
Scenario 1: Variable defined in Y is used in X
X.php
<?php
include 'web/y.php';
echo $test;
?>
Y.php
<?php
$test = 'Hello World';
?>
My Expectation: Variable $test should be accessible in X.php after the include statement
Scenario 2: Value printed in Y is used in X
X.php
<?php
include 'web/y.php';
?>
Y.php
<?php
echo 'Hello World';
?>
My Expectation: Hello World should be printed when X.php is accessed
Both work exactly as you want, i.e. output
Hello World.If they didn't work for you, probably, you gave wrong path to
y.php.The directory structure must be as follows:
If you have these files in the same directory, then you shouldn't put
web/in front ofy.php.