PHP Variables Scope
GLOBAL SCOPE = Variable declared outside function and only can be accessed outside the function.
<?php
$x = "global scope!!"; // global scope
function test1() {
echo "<p>Variable $x will generate error</p>";
// Call $x from this function will generate error, because this variable is declared outside this function
}
myTest();
echo "<p>$x will be shown here for sure because this echo is made outside the function</p>";
?>
LOCAL SCOPE = Variable declared inside function and only can be accessed inside the function.
<?php
function test2() {
$x = "local scope!!"; // local scope
echo "<p>$x will be shown here for sure because this echo is made within the function</p>";
}
myTest();
echo "<p>Variable $x will generate error</p>";
// Call $x here will generate error, because the variable is declared inside test2 function
?>