Jump to content

Global keyword: using the same value in a variable


RICH_WEB
 Share

Recommended Posts

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?

 

 

 

 

 

Link to comment
Share on other sites

Ok, so if your saying that the result of whats on the right side is stored in the left side (variable + variable will be stored in variable after = sign) then to prove this would you not change 'y = x + y' to 'y+y = x + y' and expect the result of the right side to be stored in the variables on the left side. I would then expect to see (as your going from left to right) x + y = y (5+10=15) so x + y = y + y(5+10= 15 + 15) the echo y would be 30. But this is not the result you get. The result of, 'y+y = x+y' is 15.

Edited by RICH_WEB
Link to comment
Share on other sites

not sure what you are saying but "="

 

 

The basic assignment operator is "=". Your first inclination might be to think of this as "equal to". Don't. It really means that the left operand gets set to the value of the expression on the right (that is, "gets set to"). http://php.net/manual/en/language.operators.assignment.php

 

and i tried this code :

<?php 
$a=1;
$b=2;
$c=3;
$d=4;
$a+$b=$c+$d;
echo"a = $a b= $b c= $c d= $d";
?>

the result was a = 1 b= 7 c= 3 d= 4 .

 

as you can see the $a stayed the same no value assigned to it after the $a+$b=$c+$d; only $b gets a new value .

hope this explains it

Link to comment
Share on other sites

Here is the list of operators and their associativity and precedence, for more information:

 

http://php.net/manual/en/language.operators.precedence.php

 

Operator precedence lists which operators get evaluated first in an expression. Associativity lists in which direction the operators get evaluated, some are evaluated from left to right and others from right to left. Assignment operators are right-associative, so everything to the right of the assignment operator gets evaluated first, then the assignment operator gets evaluated. Note that the arithmetic operators have a higher precedence than the assignment operators, so they will get evaluated before the assignment operators. So if you have this:

 

$a=1;
$b=2;
$c=3;
$d=4;
$a=$a+$b=$c+$d;
Then it will execute the last line kind of like this:

 

$a=$a+$b=$c+$d;
$a=$a+$b=3+4;
$a=$a+$b=7;
$a=$a+($b=7); // assign 7 to $b
$a=$a+$b;
$a=$a+7;
$a=1+7;
$a=8;
So after that runs, $a will be 8, $b will be 7, and $c and $d will be unchanged.
Link to comment
Share on other sites

You can usually avoid the confusion of operator precedence by using parenthesis to clarify your intent...

 

$a = ($b + $c)/($d + $e);

 

"global" is used to declare that these variables are the existing global variables and not new local variables.

 

$a = $b is an assignment of a value to $a.

 

$a == $b is a logical test of equivalence which might be used in an if statement.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...