0

Why is the following code "crashing" in PHP?

$normal_array       = array();
$array_of_arrayrefs = array( &$normal_array );

end( $array_of_arrayrefs )["one"] = 1; // choking on this one

The expected result is that the final code line appends $normal_array with key one having value 1 but there is no output what so ever, not even prints preceeding this code. In the real context of this scenario I use end() function to always append to the last array reference.

4

1 に答える 1

3

これはクラッシュしません。構文エラーが含まれているだけです。

end( $array_of_arrayrefs )["one"] = 1;

残念ながら、PHP では関数の戻り値を配列として扱うことはできません。値を明示的に割り当てる必要があります。end残念ながら、これは戻り値のコピーを作成するため、ここでは機能しません。

于 2008-10-10T15:08:05.890 に答える