Hi,
I'm a bit confused as to the example, given on the w3chool website about learning PHP, about 'Global Keywords'.
The example is as follows:
line1 - <?php
line2 - $x = 5; line3 - $y = 10;
line4 -
line 5 - function myTest() {
line 6 - global $x, $y;
line 7 - $y = $x + $y;
" " 8 - }
" " 9 -
" " 10 - myTest();
" " 11- echo $y; // outputs 15 " " 12 - ?>
To me the example goes as follows:
line 2 and 3; you set the variables outside of the function - the global variable.
line 6; Then you use the global to [call?] the global variables set in line 2 and 3.
line 7; You then use $y twice.
Line 7 is the bit that is confusing. How can you assign 10 to y and 5 to x then have 'y = x + y'? To me this would read 10 = 5+10 as you have already assigned y = 10.
Can some please explain how you are able to use $y as 10 but then $y is also 15?
Would it be correct as to say the variables in line 7 are read from right to left?
The global variable has stored the value of y to equal 10. Then inside the function the value stored in the variable x and y are added together to then change/update the value of the variable y to 15 (but only inside that function)? So line 7 is read in the php code from left to right so that the variable y is updated with the new value 15? So does that mean that variables used in the way equivalent to line 7, ie variable = variable + variable will always be read from right to left?