4

I have the following code:

$g_value = 'something';
print "$g_value";

function get_value() {

    global $g_value;
    print $g_value;
}

print get_value();

When I run it in a stand-alone PHP script, I get 'somethingsomething'. However, when I run it in a WordPress plugin, I only get 'something'- the global declaration does not make the var accessible in the function. I thought this should always work, and isn't dependent on register_globals or any other environment setting. What's going on here?

1 Answer 1

11
global $g_value;  //declare it global even before assigning it., this should fix it.

$g_value = 'something';
print "$g_value";

function get_value() {

    global $g_value;
    print $g_value;
}

print get_value();
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.