This had me stumped for a while – using some 3rd party code with PHP 5.3.0, the “global” keyword didn’t seem to work any more (references to the supposedly “global” variable from within functions always gave NULL)..
You have to declare the variable as “global” before setting it in the outer scope – then it works again.
So, this used to work (but doesn’t under PHP 5.3.0) :
$util = new Utility(); global $util; function show() { global $util; echo "$util->version"; }
but if you swap the lines of the declaration of $utils, then it does work under PHP 5.3.0 :
global $util; $util = new Utility(); function show() { global $util; echo "$util->version"; }
man off course global still works on php 5.3.
at least it works on 5.3.5
cheers
I think this was specifically a problem with PHP 5.3.0 – in later versions (certainly PHP 5.3.2+) it works as expected.
See also http://programisiai.lt/index.php/topic,1426.msg4171.html
thanks! i was wondering why it didn’t seem to work any more!
Thumbs up!
Thanks a lot! This saved me from a lot of dispair!
How about if the variable is set in a function
function processFile ($file, $count)
$count is incremented in the function when each record is added to a table, however I cant access this variable outside the function, it’s driving me mad!
Wowwwww thank you, it made me mad !!
Is there a possibility that this script was included from somewhere else as a function or something?
Thanks a lot, man! you just saved me from bashing my head against the desk!
BTW: I recently upgraded to PHP 5.6.8 and encountered this exact same issue.
OK I’m sorry but this was just lack of attention. You have used the variable before declaring it global, which means it was created by PHP at local scope. This is not PHP error, it is your, even if it worked before, it is not designed to work this way 😉