• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.

RESOLVED Can global variables be used?

Maple

New member
Messages
1
Likes
0
Points
1
#1
I have the following test PFP snippet. The output from the echo statement shows that the variable has a value of 1. The output from the function says NUL. I realize I could pass the variable as a function argument. This is just a simple test case to see if global variables are supported. I have a larger project that I'm attempting to move into Woody Ad Snippets that relies on global variables. It'll be a bit of work to modify the code to pass these variables as arguments if that's what I need to do.

$testvar = 1;
function testfunc() {
global $testvar;
var_dump ($testvar);
}
echo "testvar is [$testvar]<br>\n";
testfunc();
 

Temyk

Support team
Staff member
Messages
325
Likes
20
Points
18
#2
Hello.

You must declare the variable global.
PHP:
global $testvar;
$testvar = 1;

function testfunc() {
    global $testvar;
    var_dump ($testvar);
}

echo "testvar is [$testvar]<br>\n";

testfunc();