Php Tutorial assignment operator
The basic assignment operator is "= ". At first, we may think it is "equal", but it is not. It actually means assigning the value of the right expression to the number of operations on the left.
The value of the value assignment expression is the value assigned. That is to say, the value of "$ a = 3" is 3. In this way, you can do some tips:
<? Php
$ A = ($ B = 4) + 5; // $ a is now 9, while $ B is 4.
?>
<? Php
$ A = 10;
$ B = 20;
Echo $ a + = $ B; // equivalent to $ a = $ a + $ B 10 + 20 = 30
Echo "<br/> ";
Echo $ a-= $ B; // equivalent to $ a = $ a-$ B 30-20 = 10
Echo "<br/> ";
Echo $ a * = $ B; // equivalent to $ a = $ a * $ B 10*20 = 200
Echo "<br/> ";
Echo $ a/= $ B; // equivalent to $ a = $ a/$ B 200/20 = 10
Echo "<br/> ";
Echo $ a % = $ B; // equivalent to $ a = $ a % $ B 10% 20 = 10
Echo "<br/> ";
// See the String Operators page
Echo $ a. = $ B; // equivalent to $ a = $ a. $ B 10.20 = 1020
Echo "<br/> ";
$ A = true;
$ B = false;
// See the Bitwise Operators page
Echo $ a & = $ B; // equivalent to $ a = $ a & $ B 0 true & false = false
Echo "<br/> ";
Echo $ a | = $ B; // equivalent to $ a = $ a | $ B 0 false | false = false
Echo "<br/> ";
Echo $ a ^ = $ B; // equivalent to $ a = $ a ^ $ B 0 false ^ false = false
Echo "<br/> ";
$ A = 1;
$ B = 2;
Echo $ a <= $ B; // equivalent to $ a = $ a <$ B 1 <2 = 4
Echo "<br/> ";
Echo $ a >=$ B; // equivalent to $ a =a >>>4 B 4 >> 2 = 1
Echo "<br/> ";
?>
In addition to the basic value assignment operators, there are also "combined operators" suitable for all binary arithmetic, array sets, and string operators ", in this way, you can use its value in an expression and assign the result of the expression to it. For example:
<? Php
$ A = 3;
$ A + = 5; // sets $ a to 8, as if we had said: $ a = $ a + 5;
$ B = "Hello ";
$ B. = "There! "; // Sets $ B to" Hello There! ", Just like $ B = $ B." There! ";
?>
Note that the value assignment operation copies the value of the original variable to the new variable (pass the value assignment), so changing one does not affect the other. This is also suitable for copying some values such as large arrays in intensive loops. You can also assign values by reference using the $ var = & $ othervar; syntax. Referencing a value assignment means that both variables point to the same data without any data copies. For more information about references, see references. In PHP 5, objects are always assigned by reference unless the new clone keyword is explicitly used.