0
How can I include a php variable from an other php file?
I tried to get a variable from a php file for example file.php to work in index.php. I used the include statement, but it didn't work.
1 Answer
+ 1
Try to do this simple test. Variables from included PHP scripts should work.
Start the following PHP scripts with the usual ?php line. I can't share it in a Sololearn answer because of Sololearn's annoying answer validation bugs.
includer.php content:
include('included.php');
echo "Expected is that 2 should be printed after this: ";
echo $x;
included.php content:
$x = 2;
When I load includer.php in a browser, it responds with:
Expected is that 2 should be printed after this: 2
As you see, the variable x declared in an included PHP script is set and accessible from the includer script. Get that much to work and then troubleshoot out from that to see what part of your code base is messing up. Maybe your code sets or unsets the variable somewhere you don't expect. Maybe something in your PHP is undoing the change that your included PHP script correctly does.