
Parvez Noor
Pro Student 13,477 PointsWhy does y = 5?
The question asked what y =. I chose 5 because the parameters in the function sum_two_numbers when setting the y variable are 2,3.
Does this mean that because the y variable is called later than when the x variable is defined, that within the function, x becomes 2 and y becomes 3, and therefore y becomes the return of z (which is 5) because y = the output of the function?
What confuses me about this, though, is doesn't mean that mean in the function y=5 and x=1? wouldn't that make it 6??
It's a little confusing!
$x = 1;
function sum_two_numbers($x,$y) { $z = $x + $y; return $z; }
$y = sum_two_numbers(2,3); echo $y;
1 Answer

Steven Parker
177,616 PointsThe important thing to remember for this question is the rules of variable scope. The $x and $y inside the function are totally different from the ones outside the function.
Inside the function, you were right the first time in that $x is 2 and $y is 3; because they were passed as arguments. And arguments "shadow" (take precedence over) any globals of the same name.
But outside the function, $x is still 1 and $y gets assigned to return value from the function. And those two are never added together.