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
Operadores de Atribuição
O operador básico de atribuição é "=". A sua primeira inclinação deve ser a de pensar nisto como "é igual". Não. Isto quer dizer, na verdade, que o operando da esquerda recebe o valor da expressão da direita (ou seja, "é configurado para").
O valor de uma expressão de atribuição é o valor atribuído. Ou seja, o valor de "$a = 3" é 3. Isto permite que você faça alguns truques:
<?php
$a = ($b = 4) + 5; // $a é igual a 9 agora e $b foi configurado como 4.
?>
Além do operador básico de atribuição, há "operadores combinados" para todos os operadores aritméticos, de array e string que permitem a você pegar um valor de uma expressão e então usar seu próprio valor para o resultado daquela expressão. Por exemplo:
<?php
$a = 3;
$a += 5; // configura $a para 8, como se disséssemos: $a = $a + 5;
$b = "Bom ";
$b .= "Dia!"; // configura $b para "Bom Dia!", como em $b = $b . "Dia!";
?>
Note que a atribuição copia a variável original para a nova (atribuição por valor), assim a mudança de uma não afeta a outra. Isto pode ter relevância se você precisa copiar algo como uma grande matriz dentro de um loop longo. Atribuições por referência é também suportada, usando a sintaxe $var = &$outra_var;. 'Atribuição por referência' significa que ambas as variáveis acabam apontando para os mesmos dados, e nada é copiado para lugar nenhum. Para aprender mais sobre referências, leia Referências explicadas. No PHP 5, objetos são atribuídos por referência a menos que explícitamente feito o contrário com a nova palavra chave clone.
You could also take adam at gmail dot com's xor-assignment operator and use the fact that it's right-associative:
$a ^= $b ^= $a ^= $b;
[[ Editor's note: You are much better off using the foreach (array_expression as $key => $value) control structure in this case ]]
When using
<php
while ($var = current($array) {
#do stuff
next($aray)
?>
to process an array, if current($array) happens to be falsy but not === false it will still end the loop. In such a case strict typing must be used.
Like this:
<php
while (($var = current($array)) !== FALSE) {
#do stuff
next($aray)
?>
Of course if your array may contain actual FALSE values you will have to deal with those some other way.
bradlis7 at bradlis7 dot com's description is a bit confusing. Here it is rephrased.
<?php
$a = 'a';
$b = 'b';
$a .= $b .= "foo";
echo $a,"\n",$b;?>
outputs
abfoo
bfoo
Because the assignment operators are right-associative and evaluate to the result of the assignment
<?php
$a .= $b .= "foo";
?>
is equivalent to
<?php
$a .= ($b .= "foo");
?>
and therefore
<?php
$b .= "foo";
$a .= $b;
?>
Note whenever you do this
<?php
$a .= $b .= "bla bla";
?>
it comes out to be the same as the following:
<?php
$a .= $b."bla bla";
$b .= "bla bla";
?>
So $a actually becomes $a and the final $b string. I'm sure it's the same with numerical assignments (+=, *=...).
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.
or you could use the xor-assignment operator..
$a ^= $b;
$b ^= $a;
$a ^= $b;
