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