-3

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".

Chris Middleton
  • 5,654
  • 5
  • 31
  • 68
  • 2
    This is *basic* scope here. `$debug` is *not* in scope. Why do you think it is? – John Conde Jul 22 '14 at 18:10
  • @JohnConde It is at the same level as the function itself. How is that not in scope? – Chris Middleton Jul 22 '14 at 18:10
  • 2
    This isn't JavaScript. Variables declared outside of a function are out of scope. [Documentation](http://php.net/manual/en/language.variables.scope.php). – John Conde Jul 22 '14 at 18:11
  • You can globalize the debug variabele but that not a good practice. However, if you use classes then it will be accessible via $this->debug. – Stefan Jul 22 '14 at 18:12
  • I see. So I have to include global inside the function itself. Sorry, I have read the documentation on this before and I guess I got it mixed up. – Chris Middleton Jul 22 '14 at 18:13
  • 1
    See also my answer here: http://stackoverflow.com/questions/15687363/php-access-global-variable-in-function/15687405#15687405 – Matteo Tassinari Jul 22 '14 at 18:41

2 Answers2

1

Please just use following code:

    <?php

    $debug = true;

    function myFunction($debug)
    {

        if($debug) {
            echo "I'm debugging";
        } else {
            echo "I'm not debugging";
        }
    }

    myFunction($debug);

    ?>

The function requires the parameter "$debug" to work with it. Welcome to php! :)

Tyralcori
  • 1,079
  • 13
  • 33
  • The actual function `myFunction` has many more parameters and I don't want to add `$debug` to the list. The global solution is what I needed in this instance. Thanks for the suggestion though. – Chris Middleton Jul 22 '14 at 18:14
  • Then write a getter / setter for this.. i know this this problem, and it's the most simple way to solve it.. – Tyralcori Jul 22 '14 at 18:15
  • I ended up using a global, but I think the solution of passing it as a parameter is the best one (best-practices-wise), so I accepted your answer. – Chris Middleton Jul 22 '14 at 21:03
0

$debug = true; is outside of the scope, try this instead:

<?php



function myFunction()
{

   $debug = true;

    if($debug) {
        echo "I'm debugging";
    } else {
        echo "I'm not debugging";
    }
}

myFunction();

?>

You could also use global

user3849925
  • 31
  • 1
  • 5
  • At least, his case already have a global scope.. – Tyralcori Jul 22 '14 at 18:20
  • @Tyralcori He's not using global in his example, I'm not sure what you're talking about. It depends on what he wants to do. He could also pass the variable in function arguments. – user3849925 Jul 22 '14 at 18:23