2008-10-02 (木)
■ PHP の switch 文は怖くて使えない
PHP の == 演算子が地雷なのは知っていましたが、 まさか switch 文にも同じ問題があるとは思いませんでした。 これでピンと来ない人は、先に以下のページでも読んで下さい。 PHP が落とし穴だらけの言語であることが分かります。
さて、私が今回はまりそうになったのはこういうコードです。
function filter($value) {
switch ($value) {
case 'abc':
case 'def':
return $value;
break;
default:
return '';
break;
}
}
要するに、入力値として abc と def だけ受け付けようという意図なんですが、 このコードにはバグがありまして、
php > echo filter(0); 0
このように入力値としてゼロという数値を与えると、返り値もゼロになります。 えーっ! と思いマニュアルを読んでみると、
注意: switch/case が行うのは、緩やかな比較 であることに注意しましょう。
としっかり書かれていました。 コメント欄にも、こういう危なっかしいコードの例や対処法が山ほど載っていました。
それで私は結局、
function filter($value) {
if ($value === 'abc' || $value === 'def') {
return $value;
}
return '';
}
のように if 文と === 演算子を使うようにしました。 switch 文はもう怖くて使えません。
本日のリンク元
- 604 google検索(php switch)
- 257 google検索(php switch)
- 188 google検索(php switch)
- 175 google検索(php switch)
- 132 http://www.google.co.jp/hws/search?hl=ja&q=php swi...
- 117 google検索(f)
- 86 google検索(php switch)
- 82 google検索(php switch)
- 73 google検索(php switch)
- 68 google検索(php switch)
- 57 google検索(switch php)
- 56 google検索(PHP switch)
- 49 google検索(php switch)
- 47 google検索(php switch)
- 42 google検索(php switch文)
- 41 google検索(php switch)
- 39 google検索(php switch)
- 37 http://www.google.co.jp/
- 34 google検索(php switch文)
- 33 google検索(php switch)
- 32 google検索(switch php)
- 32 http://www.google.co.jp/hws/search?hl=ja&q=PHP swi...
- 31 google検索(php switch)
- 30 google検索(php switch)
- 30 http://d.hatena.ne.jp/m_norii/20081006/1223307774
- 29 google検索(PHP switch)
- 29 google検索(php switch)
- 27 google検索(php switch)
- 26 google検索(PHP switch)
- 23 google検索(php switch)
- 23 google検索(switch php)
- 23 google検索(switch文)
- 22 google検索(PHP switch)
- 22 google検索(php switch)
- 21 google検索(PHP switch)
- 21 google検索(php switch)
- 20 google検索(php switch)
- 19 google検索(php switch)
- 18 google検索(php switch)
- 18 google検索(php switch)
- 18 google検索(php switch)
- 17 google検索(switch php)
- 17 google検索(php switch文)
- 17 google検索(php switch)
- 17 google検索(f)
- 16 http://www.revulo.com/
- 16 google検索(php switch)
- 16 google検索(php switch)
- 15 google検索(0)
- 15 google検索(0)
- 14 google検索(PHP switch)
- 14 google検索(php switch)
- 14 google検索(switch php)
- 13 google検索(switch php)
- 13 google検索(php swich)
- 13 google検索(PHP switch)
- 13 google検索(php switch)
- 13 http://www.google.co.jp/hws/search?br=&client=fenr...
- 13 http://websearch.rakuten.co.jp/?tool_id=1&rid=2000...
- 13 http://s.luna.tv/search.aspx?client=lunascape&s=0&...
- 12 http://www.google.co.jp/url?sa=t&source=web&cd=3&v...
- 12 google検索(php switch if)
- 12 google検索(php switch)
- 12 google検索(php switch文)
- 12 http://www.google.co.jp/hws/search?hl=ja&q=php swi...
- 11 google検索(php switch)
- 11 google検索(php switch文)
- 11 google検索(switch php)
- 11 google検索(php switch)
- 11 google検索(php switch)
- 11 google検索(php switch)
- 11 http://www.google.co.jp/hws/search?hl=ja&q=switch ...
- 10 google検索(php switch)
- 10 google検索(php switch)
- 10 google検索(php switch )
- 10 google検索(switch php)
- 10 google検索(php for文)
- 10 google検索(php for文)
- 10 google検索(php スイッチ文)
- 10 google検索(f)
- 10 google検索(f)
- 10 http://d.hatena.ne.jp/f-star/20081006/p26
- 10 http://d.hatena.ne.jp/f-star/
- 9 http://www.revulo.com/notify-Notify_user_ifilter?a...
- 9 google検索(php switch)
- 9 google検索(php switch)
- 9 google検索(php switch)
- 9 google検索(php switch)
- 9 google検索(php swich)
- 9 google検索(php switch)
- 9 google検索(php switch)
- 9 google検索(php switch文)
- 9 google検索(php switch文)
- 9 google検索(php switch)
- 9 google検索(php switch)
- 9 google検索(php switch文)
- 8 google検索(php switch)
- 8 google検索(php switch if)
- 8 google検索(php switch)
- 8 google検索(php switch)

PHPプログラムは落とし穴だらけと書かれていますが、マニュアルに書かれていることを単に理解されていなかったあなたに問題があるのでは?挙動や例文まで記載されているのに、それをトラップみたいに指し示すこの記事に疑問を感じましたよ。
PHPを批判する記事をたまに見ますが、それだったらPerlとかPythonで書けばいいのに・・・。批判してまでPHPを選択する理由はなんなんでしょうか?
この記事は、PHP を批判する記事ではありません。私の失敗談を書いて、皆さんも気を付けましょうと言っているのです。