用户评论:
haubertj at alfredstate dot edu (2011-09-01 07:19:40)
[[ Editor's note: You are much better off using the foreach (array_expression as $key => $value) control structure in this case ]]
When using
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:
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.
Peter, Moscow (2011-02-11 01:44:54)
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.
Hayley Watson (2008-02-05 17:54:11)
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;
Hayley Watson (2007-10-07 15:22:17)
bradlis7 at bradlis7 dot com's description is a bit confusing. Here it is rephrased.
$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
$a.=$b.="foo";?>is equivalent to
$a.= ($b.="foo");?>and therefore
$b.="foo";$a.=$b;?>
adam at gmail dot com (2006-08-25 10:38:21)
or you could use the xor-assignment operator..
$a ^= $b;
$b ^= $a;
$a ^= $b;
bradlis7 at bradlis7 dot com (2005-08-15 08:13:01)
Note whenever you do this
$a.=$b.="bla bla";?>
it comes out to be the same as the following:
$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 (+=, *=...).
straz at mac dot nospam dot com (2004-02-20 22:18:19)
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