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
Zuweisungsoperatoren
Der einfachste Zuweisungsoperator ist "=". Wahrscheinlich kommt man als erstes auf die Idee, ihn mit "ist gleich" zu bezeichnen. Das ist falsch. In Wirklichkeit bedeutet er, dass dem linken Operanden der Wert des Ausdrucks auf der rechten Seite zugewiesen wird (man müsste ihn also mit "wird gesetzt auf den Wert von" übersetzen).
Der Wert eines Zuweisungs-Ausdruckes ist der zugewiesene Wert. D.h. der Wert des Ausdruckes "$a = 3" ist 3. Das erlaubt es, einige raffinierte Dinge anzustellen:
<?php
$a = ($b = 4) + 5; // $a ist nun gleich 9 und $b wurde auf den Wert 4 gesetzt.
?>
Zusätzlich zu dem oben vorgestellten Zuweisungsoperator "=" gibt es "kombinierte Operatoren" für alle binären, arithmetischen und String-Operatoren, die es erlauben, den Wert einer Variablen in einem Ausdruck zu benutzen, und dieser anschließend das Ergebnis des Ausdrucks als neuen Wert zuzuweisen. Zum Beispiel:
<?php
$a = 3;
$a += 5; // setzt $a auf den Wert 8, als ob wir geschrieben haetten: $a = $a + 5;
$b = "Hallo ";
$b .= "Du!"; // setzt $b auf den Wert "Hallo Du!", aequivalent zu
// $b = $b . "Du!";
?>
Man beachte, dass die Zuweisung nur den Wert der Ursprungsvarialbe der neuen Variable zuweist (Zuweisung als Wert, sie "kopiert"), weshalb sich Änderungen an der einen Variablen nicht auf die andere auswirken werden. Das kann wichtig sein, wenn man ein großes Array o. ä. in einer Schleife kopieren muss. Beginnend mit PHP 4 wird 'assignement by reference' (Zuweisung als Verweis), mit Hilfe der Schreibweise $var =&$othervar; unterstützt , das funktioniert jedoch nicht in PHP 3. 'Assignement by reference' bedeutet, dass beide Variablen nach der Zuweisung die selben Daten repräsentieren und nichts kopiert wird. Um mehr über Referenzen zu lernen, lesen Sie bitte den Abschnitt Referenzen erklärt.
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;
