I'm using a $debug
variable to control print statements in a function I'm testing. Even though $debug
is declared and initialized to true
at the same scope level as the function, it is being treated as false
(or undefined
, I guess) inside the function. Is my understanding of variable scope in PHP wrong? How do I fix this?
My actual code is much more complex, but the following is a minimal example which exhibits the same problem. For what it's worth, I'm running this on the command line with PHP 5.4.24 on Mac OS X 10.9.4.
<?php
$debug = true;
function myFunction()
{
if($debug) {
echo "I'm debugging";
} else {
echo "I'm not debugging";
}
}
myFunction();
?>
For me this function displays "I'm not debugging".