search for in the  
<Arithmetic OperatorsBitwise Operators>
Last updated: Thu, 19 May 2005

Assignment Operators

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 rights (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:

<?php

$a
= ($b = 4) + 5; // $a is equal to 9 now, and $b has been set to 4.

?>

In addition to the basic assignment operator, there are "combined operators" for all of the binary arithmetic 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:

<?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 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. Since PHP 4, assignment by reference has been supported, using the $var = &$othervar; syntax, but this is not possible in PHP 3. 'Assignment by reference' means that both variables end up pointing at the same data, and nothing is copied anywhere. To learn more about references, please read References explained.



User Contributed Notes
Assignment Operators
trosos at atlas dot cz
03-Jul-2004 08:19
Note that the value of an assignment expression is not the value assigned, but the variable itself (after assign, of course). This allows to do something like this:

<?php

function f(&$a)
{
 ...
}

f($v=3);

?>
straz at mac dot nospam dot com
21-Feb-2004 12:18
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    $a = $a << $b    Left shift
$a >>= $b    $a = $a >> $b      Right shift
jeronimo at DELETE_THIS dot transartmedia dot com
28-Jan-2004 09:27
If you want to swap values between variables without using an intermediary, try using the list() and array() language constructs. For instance:

<?

// Initial values.
$biggest = 1;
$smallest = 10;

// Instead of using a temporary variable...
$temp = $biggest;
$biggest = $smallest;
$smallest = $temp;

// ...Just swap the values.
list($biggest, $smallest) = array($smallest, $biggest);

?>

This works with any number of variables; you're not limited to just swapping two.
Cheers,
Jeronimo

<Arithmetic OperatorsBitwise Operators>
 Last updated: Thu, 19 May 2005
Copyright © 2001-2005 The PHP Group
All rights reserved.
This unofficial mirror is operated at: The Server Pages
Last updated: Thu May 19 17:35:34 2005 CDT