And you? When will you begin that long journey into yourself? Rumi
Idea Transcript
Downloads Documentation Get Involved Help Search PHP 5.6.33 Released Getting Started Introduction A simple tutorial Language Reference Basic syntax Types Variables Constants Expressions Operators Control Structures Functions Classes and Objects Namespaces Errors Exceptions Generators References Explained Predefined Variables Predefined Exceptions Predefined Interfaces and Classes Context options and parameters Supported Protocols and Wrappers Security Introduction General considerations Installed as CGI binary Installed as an Apache module Session Security Filesystem Security 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"). The value of an assignment expression is the value assigned. That is, the value of "$a = 3" is 3. This allows you to do some tricky things:
In addition to the basic assignment operator, there are "combined operators" for all of the binary arithmetic, array union and string operators that allow you to use a value in an expression and then set its value to the result of that expression. For example:
Note that the assignment copies the original variable to the new one (assignment by value), so changes to one will not affect the other. This may also have relevance if you need to copy something like a large array inside a tight loop. An exception to the usual assignment by value behaviour within PHP occurs with objects, which are assigned by reference in PHP 5. Objects may be explicitly copied via the clone keyword.
Assignment by Reference ¶ Assignment by reference is also supported, using the "$var = &$othervar;" syntax. Assignment by reference means that both variables end up pointing at the same data, and nothing is copied anywhere. Example #1 Assigning by reference
As of PHP 5, the new operator returns a reference automatically, so assigning the result of new by reference results in an E_DEPRECATED message in PHP 5.3 and later, and an E_STRICT message in earlier versions. For example, this code will result in a warning:
More information on references and their potential uses can be found in the References Explained section of the manual. add a note
User Contributed Notes 9 notes up down 279 straz at mac dot nospam dot com ¶ 13 years ago This page really ought to have table of assignment operators, namely, See the Arithmetic Operators page (http://www.php.net/manual/en/language.operators.arithmetic.php) Assignment Same as: $a += $b $a = $a + $b Addition $a -= $b $a = $a - $b Subtraction $a *= $b $a = $a * $b Multiplication $a /= $b $a = $a / $b Division $a %= $b $a = $a % $b Modulus See the String Operators page(http://www.php.net/manual/en/language.operators.string.php) $a .= $b $a = $a . $b Concatenate See the Bitwise Operators page (http://www.php.net/manual/en/language.operators.bitwise.php) $a &= $b $a = $a & $b Bitwise And $a |= $b $a = $a | $b Bitwise Or $a ^= $b $a = $a ^ $b Bitwise Xor $a > $b Right shift
up down 50 Peter, Moscow ¶ 6 years ago Using $text .= "additional text"; instead of $text = $text ."additional text"; can seriously enhance performance due to memory allocation efficiency. I reduced execution time from 5 sec to .5 sec (10 times) by simply switching to the first pattern for a loop with 900 iterations over a string $text that reaches 800K by the end.
up down 21 Robert Schneider ¶ 2 years ago Be aware of assignments with conditionals. The assignment operator is stronger as 'and', 'or' and 'xor'.
up down 16 Hayley Watson ¶ 10 years ago bradlis7 at bradlis7 dot com's description is a bit confusing. Here it is rephrased. outputs abfoo bfoo Because the assignment operators are right-associative and evaluate to the result of the assignment is equivalent to and therefore
up down -9 ma dot bx dot ar at gamil dot com ¶ 3 years ago
up down -6 asc at putc dot de ¶ 2 years ago
up down -7 Hayley Watson ¶ 9 years ago
up down -18 bradlis7 at bradlis7 dot com ¶ 12 years ago
up down -24 haubertj at alfredstate dot edu ¶ 6 years ago