troubleshooting Question

Global variable in php function stays unset

Avatar of edgarsabolins
edgarsabolins asked on
PHP
20 Comments1 Solution1023 ViewsLast Modified:
Recently i faced with one interesting problem. I gave to my friend one of my php functions, which is using one global variable. Thatl variable is defined straight before the function. In fact, a simple example of this function is in this code fragment.

So, where is the problem? I copy this code, and paste it in one of my PHPBB site's files, but after executing the page i see a message "ERROR" which i thought is impossible, because a global variable has been set.

Please try to explain me this - why previously (just before the function)  defined variable, dont shows up in function and stays unset, if it's defined in function as global? Is it some PHP anomaly or what?
<?php
$variable = 'AAABBBCCCC';
 
function test() {
 
global $variable;
 
if(!isset($variable)) {
echo "ERROR!";
} else {
echo "ALL SEEMS OK!";
}
 
}
 
test();
?>
ASKER CERTIFIED SOLUTION
Join our community to see this answer!
Unlock 1 Answer and 20 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 20 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros