4

I have:

include ('functions.php');
check_blocked();
echo $blocked;

in functions.php, check_blocked(); exists. Inside check_blocked I got:

global $blocked;
$blocked = '1234';

I want to echo $blocked variable, that are inside check_blocked().

It doesnt work, no output..

This is an example of my original problem, so please dont say that I could just have the echo inside the function, as I cannot have in my original code.

4
  • Try using $GLOBALS instead and see if that helps. Commented Jan 13, 2011 at 19:47
  • 4
    why are all of you encouraging him to use globals, when he should just return the value from the function?? Commented Jan 13, 2011 at 19:49
  • 2
    Return $blocked? So that 'echo check_blocked()' actually works? If there are multiple $blocked, then use arrays. Where's the problem? Commented Jan 13, 2011 at 19:49
  • @dqhendricks Crazy how 18 seconds can matter... Commented Jan 13, 2011 at 19:50

5 Answers 5

4

Your code should look like

$blocked = 0;
include('functions.php');
check_blocked();
echo $blocked;

With functions.php looking like

function check_blocked(){
     global $blocked;
     $blocked = 1234;
}

You must define blocked outside of the scope of the function before you global blocked into the function.

Sign up to request clarification or add additional context in comments.

5 Comments

+1. Indeed. Otherwise the global $blocked in the function doesn't do anything; it doesn't find a global named $blocked because you did not make one yet!
@Tomalak Geret'kal upvoting the use of globals? why not just return the value from the function?
@charlie If you run it, it will echo 1234 because your now in the proper scope. Had $blocked not been set to zero, then you would see nothing. If $blocked was echoed before the check_blocked(); function was called, then you would see zero.
@dqhendricks That could possibly be a superior approach (depending on the actual scenario behind the testcase). However, this answer is a correct response to the question asked, identifying the specific code mistake committed by the OP.
There are 100 ways to skin a cat, this answer actually answers the question asked about scope of variables.
4

Why not just returning the value?

function check_blocked() {
    $blocked = '1234';
    return $blocked;
}

then

include ('functions.php');
echo check_blocked();

Avoid globales wherever possible.

4 Comments

+1. Globals should be avoided. This is the correct way to do it.
@mfonda There is no single "correct" way to do anything, and sweeping generalisations don't help.
We don't know enough about the real-life scenario behind his testcase. This may in fact not be appropriate; I feel that you diverged too far from the original testcase. No downvote, though.
@Tomalak Geret'kal: Well that is why I asked (;)). I agree, to give a more appropriate answer we would need more information. Anyway, it is always good to show other possibilities imo.
0

You can change check_blocked() to return $blocked or you can try use:

include ('functions.php');
check_blocked();
global $blocked;
echo $blocked;

1 Comment

-1. Eh? That's already in global scope. Why write global there? What nonsense.
0

instead, use a return value. globals are bad.

include ('functions.php');
echo check_blocked();

function check_blocked() {
    return '1234';
}

3 Comments

Though a valid way to do things, doesn't push the point of variable scope and how to actually use a global variable.
@Geoffrey Wagner he doesn't actually ask anything about globals or scope... the OPs question was how to echo a variable that is created within a function after the function has executed. why not help them to understand the best method?
i see your point completely and i am not devaluing, but why not show them the errors of their ways instead of saying, just do it this way?? Teach a man to fish and feed him forever IMO
0

Unless the variable is declared outside of the check_blocked() function, you can't.

$foo = null;

function check_blocked() {
   global $foo;
   $foo = "Hello";
   // do some stuff
}

global $foo;
echo $foo; // prints "Hello"

If its defined inside the function like:

function check_blocked() {
    global $foo;
    $foo = "Hello";
    // other work
}


global $foo;
echo $foo; // Nothing

then the variable is scoped locally, and is discarded as soon as the function completed.

1 Comment

) Happens to us all. If you also add it to the second example, and explain why it no longer works properly (which is what this question is all about), then I will remove my downvote.

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.