0

Why isn't the "yes you are in check function" printed?

file.php

 $debug = true;

 function check() {

     global $debug;

     error_log("in check function");

     if ($debug) {
         error_log(" yes you are in check function"); 
     }
}

SO when check() is called, the output is ONLY:

in check function

how can that be? why isn't the 2nd error log line printed?

user1864734
  • 135
  • 3
  • 13
  • 3
    Can't reproduce https://3v4l.org/VGZqF – Nick Jan 24 '20 at 05:21
  • 1
    Make sur all of this is done at a global scope otherwise define $debug gloabally before initilialising it (global $debug; $debug = true;) – Jordan Daigle Jan 24 '20 at 05:36
  • thanks Nick, but it's not working on my server, in my code???? I was positive I was correct :) but???? thanks Jordan, are you saying to use global twice? On the outside and inside the function? – user1864734 Jan 24 '20 at 05:55
  • Answer provided here might be relevant. https://stackoverflow.com/a/5355688/8448583 States that the `global` keyword is used to access global variables, not create them. If that's the case, you don't have a global. – David Tran Jan 24 '20 at 06:22
  • 1
    If this code is inside another function therefore isn't in a global scope then you would need to add "gobal $debug;" before setting it to true. Beside that the only other possibilty I could think of would be that the debug variable is used somewhere else. – Jordan Daigle Jan 24 '20 at 13:59

0 Answers0