1

I need help understanding this PHP code example:

$bar = "outside";
function foo($bar) {
  $bar = "inside";
  return $bar;
}
$bar = foo($bar);
echo $bar

I predicted $bar would echo "outside", but it echoed "inside" and I don't see why. $bar initially has the value "outside" (defined as a global). Then the function foo is called (right after it's defined), and takes a parameter, which also happens to be called $bar. $bar was given the value "outside", so the value "outside" is passed into foo. Then we have the statement $bar = "inside", which doesn't make sense to me, because it seems to mean "outside" = "inside", and how can you assign a string to another string? Then the function returns $bar, which I think should have the value "outside" since that's what was passed to foo. How it acquired the value "inside" I can't figure out.

Edit: My question isn't about variable scope. What I'm wondering is why the parameter value isn't being passed to the function on line 3, giving the nonsensical statement "outside" = "inside". Based on the answers I've gotten, I can only assume it's because such a statement is illegal in PHP (and probably all other languages), so the interpreter simply doesn't do this (despite the fact that the interpreter's normal behavior is to substitute parameters wherever they occur in the function body).

Thank you

13
  • 1
    This is explained (with examples) here: Variable scope - you need to understand it first to make use of it, it's a very basic principle across many, many computer languages. Commented Aug 8, 2011 at 16:24
  • you should be more clear about what you need with that function. Commented Aug 8, 2011 at 16:59
  • @hakre: My question doesn't have to do with variable scope. I understand scope well enough to deal with this problem. Where my confusion is arising is why the parameter value "outside" isn't being substituted for the local variable $bar on line 3, the way that such parameter values are normally substituted. In other words, I seem to have stumbled on a case where a parameter value is not passed into the function. Commented Aug 8, 2011 at 17:06
  • 1
    @Matthew: Fair enough -- I didn't pick up on that. I just know that it is against the rules to post "homework" questions here, and many participants here take that rule very seriously (as they should). Commented Aug 8, 2011 at 17:41
  • 2
    @Bill: Not all here are native english speakers, and I've finally understood at which part you misunderstood what the grammar of the language means. It's just that I can not make it explainable to you. I can live with that, even enlightenment is close: Expressions (PHP) Commented Aug 8, 2011 at 17:45

3 Answers 3

1

Your line $bar = "inside"; is re-assigning the value of $bar. So, no matter what value you pass in, $bar gets assigned the value "inside".

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

2 Comments

Yes, but isn't the value "outside" substituted on line 3 to the local variable $bar within the function? That creates the statement "outside" = "inside", which is what's confusing me.
No, a variable on the left-hand side of an assignment operator (=) is always on the receiving end of the assignment - it is not evaluated, only re-assigned.
1

Well, I'm no PHP expert but I've worked with a few other languages so let me take a stab:

When the function begins execution the variable $bar is local to the function (you're not referencing the global $bar on line 3). You set this local variable to "inside" and return it. The global $bar is then set to that return value, "inside".

You can see this work if you change the function declaration to:

function foo($bid) {
    $bid = "inside";
    return $bid;
}

based on the comments you've been posting on other questions I wanted to add something:

When you see "$bid" somewhere in code what happens is not variable substitution, but variable evaluation. In the case of the equal sign being on the right side of this "evaluation" the compiler knows "don't evaluate, set". Whereas when the equal sign is on the left side of the variable then it's evaluated. The compiler knows the function differently when the variable is on the left or right side of the equal sign.

2 Comments

Yeah but I'm still confused about what happens to the value that's passed into the function. "outside" is passed into foo, so doesn't that mean that "outside" is substituted for any occurrences of $bar (or $bid) inside foo? That gives the confusing assignment statement "outside" = "inside"; on line 3.
No, not quite. PHP as well as other languages don't work quite like that. It's not a substitution of values like you'd see in algebra. What the third line is saying is "set the variable $bar to 'inside'" not "set 'outside' to 'inside'"
0

1:

$bar = "outside";
function foo($bar) {
    $bar = "inside";
    return $bar;
}
$bar = foo($bar);
echo $bar // **echo's inside**

2:

$bar = "outside";
function foo($bar) {
    GLOBAL $bar;
    $bar = "inside";
    return $bar;
}
$bar = foo($bar);
echo $bar  // **echo's inside**

3:

$bar = "outside";
function foo($bar) {

  $bar = "inside";
  GLOBAL $bar;
  return $bar;
}
$bar = foo($bar);
echo $bar //  **echo's outside**

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.