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
Scenario 1: Variable defined in Y is used in X
Scenario 2: Value printed in Y is used in X
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:
x.php
web/y.php
If you have these files in the same directory, then you shouldn't put web/
in front of y.php
.