[[ 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.
代入演算子
代入演算子の基本となるものは "=" です。この演算子に関して最初に 思い付く意味は"等しい"であるかもしれません。しかし、そうではありません。 本当は、左オペランドに右オペランドの式の値を設定する("得て代入する") ことを意味します。
代入式の値は、代入される値です。つまり、"$a = 3" の値は、3 です。 これにより、以下のようなトリッキーなことができるようになります。
<?php
$a = ($b = 4) + 5; // $a は 9 に等しくなり、$b には 4 が代入されます
?>
配列の場合、キーの名前を指定して値を代入するには "=>" 演算子を使います。この演算子の 優先順位 は、その他の代入演算子と同じです。
基本代入演算子に加えて、全ての バイナリ演算子、配列結合および文字列演算子に関して 「複合演算子」があります。 これにより、式の中の値を使用し、その値をその式の結果とすることができます。 例えば、
<?php
$a = 3;
$a += 5; // $a を 8 にセットします。$a = $a + 5; と同じです。
$b = "Hello ";
$b .= "There!"; // $bを"Hello There!"にセットします。$b = $b . "There!";と同じです。
?>
代入は、元の変数を新しい変数にコピーする(値による代入)ため、 片方の変数に対する変更はもう片方に影響を与えないということに 注意してください。この動作により、密なループの内側で大きな配列のようなものを コピーする必要がある場合には問題を生じる可能性があります。
PHP では通常は値による代入になりますが、オブジェクト型については例外です。 PHP 5 以降、オブジェクトは参照で代入されるようになりました。オブジェクトを明示的にコピーするには clone キーワードを使います。
参照による代入
参照による代入もサポートしており、 "$var = &$othervar;" 構文で使うことができます。 参照による代入とは、両方の変数が同じデータを指すようにするということです。 データのコピーは発生しません。
例1 参照による代入
<?php
$a = 3;
$b = &$a; // $b は $a への参照です
print "$a\n"; // 表示: 3
print "$b\n"; // 表示: 3
$a = 4; // change $a
print "$a\n"; // 表示: 4
print "$b\n"; // 表示: 4
// $b の参照先は $a であり、その値が変わったからです
?>
PHP 5 以降では、new
演算子が自動的に参照を返すようになりました。そのため、
new の結果を参照で代入しようとすると
PHP 5.3 以降では E_DEPRECATED、それより前のバージョンでは
E_STRICT が発生します。
たとえば、このコードは警告が発生します。
<?php
class C {}
/* 次の行を実行すると、このようなエラーメッセージが出ます
* Deprecated: Assigning the return value of new by reference is deprecated in...
*/
$o = &new C;
?>
参照に関する詳細な情報やその使い方については、このマニュアルの 参照に関する説明 をご覧ください。
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.
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;
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;
?>
or you could use the xor-assignment operator..
$a ^= $b;
$b ^= $a;
$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 (+=, *=...).
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
