I am trying to access a variable defined in parent file from inside the required file, like this:
<<< parent.php >>>
$controller = 'index';
require ('child.php');
<<< child.php >>>
echo $controller;
somehow, I am receiving a null output, which means that the variable is not accessible via child.php..
am I doing something wrong here?
if possible, please let me know how I can call the above variable in my child.php file, if above seems to be wrong.
Regards
Nikhil Gupta
Is your variable declared outside a function body? If not, it needs to be.
Also, when calling the variable from your child file, try “initializing” it with the Global keyword before using it…
like
…then try printing it. Also, for debugging purposes, set the variable to a constant in your parent file instead of deriving the value in some function. In other words, make sure it’s not a function turning the value to null. (I see you are doing that in the post – but not sure if you modified it for simplicity)