foreachの罠に引っかからない

foreachで(僕が)よく引っかかる罠 | Oddwit

foreachに渡す配列変数が、配列で無い場合に、foreachからエラーが出てしまう。。
プログラムを組んでるとよくある?ことですが、以下のようにして対処するのはどうでしょう、という記事です。

[php]
foreach ( (array) $var as $key=>$value){
echo $key.” : “.$value;
}
[/php]

(array)で配列型にキャストしてしまうという。
これは良い案!早速取り入れなくては。

関連する記事:

Powered by

Leave a Comment


NOTE - You can use these HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight="">