i am relatively new to php and i’ve encountered a very strange problem.
let me just give an example of a really simple code
$test = 'test';
function test(){
echo $test;
}
test();
in the above example, it used to work just perfectly fine, returning ‘test’ as a result of calling the test() function. however, just today it started to return error saying the variable $test is undefined. i am 100 percent sure it worked just fine before. is this some update in php? or am i doing something wrong here? i am using wamp and testing this on my localhost. thanks!
This is expected behaviour, it’s called “variable scope”
read this: http://php.net/manual/en/language.variables.scope.php
One of the ‘pitfalls’ is: