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 文はもう怖くて使えません。
[コメントを書く]
本日のリンク元
- 250 google検索(php switch)
- 153 google検索(php switch)
- 95 google検索(php switch)
- 92 http://www.google.co.jp/hws/search?hl=ja&q=php swi...
- 68 google検索(php switch)
- 67 google検索(php switch)
- 57 google検索(switch php)
- 52 google検索(php switch)
- 48 google検索(php switch)
- 41 google検索(php switch)
- 39 google検索(php switch)
- 38 google検索(php switch)
- 34 google検索(php switch文)
- 32 google検索(php switch)
- 31 google検索(php switch)
- 29 google検索(php switch文)
- 29 google検索(PHP switch)
- 28 http://d.hatena.ne.jp/m_norii/20081006/1223307774
- 27 google検索(php switch)
- 23 google検索(php switch)
- 22 google検索(php switch)
- 22 http://www.google.co.jp/hws/search?hl=ja&q=PHP swi...
- 19 google検索(php switch)
- 19 google検索(f)
- 18 google検索(php switch)
- 18 google検索(php switch)
- 16 google検索(php switch)
- 15 google検索(php switch)
- 15 google検索(PHP switch)
- 15 google検索(php switch)
- 14 google検索(PHP switch)
- 14 google検索(php switch)
- 14 google検索(switch php)
- 14 google検索(switch php)
- 14 google検索(php switch)
- 13 google検索(PHP switch)
- 13 google検索(php switch)
- 12 google検索(switch php)
- 12 google検索(php switch)
- 12 google検索(PHP switch)
- 12 google検索(php switch)
- 12 google検索(php switch文)
- 11 google検索(php switch)
- 11 google検索(php switch文)
- 11 google検索(switch php)
- 11 google検索(php switch)
- 11 google検索(php switch)
- 11 google検索(php switch)
- 10 google検索(php switch)
- 10 google検索(switch php)
- 10 google検索(php switch文)
- 10 google検索(switch php)
- 10 google検索(php for文)
- 10 google検索(php for文)
- 10 google検索(php スイッチ文)
- 10 http://www.google.co.jp/hws/search?hl=ja&q=php swi...
- 10 http://d.hatena.ne.jp/f-star/
- 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 http://www.google.co.jp/hws/search?br=&client=fenr...
- 8 google検索(php switch)
- 8 google検索(php switch)
- 8 google検索(php switch if)
- 8 google検索(php switch)
- 8 google検索(php for文)
- 8 google検索(php switch文)
- 8 google検索(php switch)
- 8 google検索(PHP switch)
- 8 google検索(php switch)
- 8 http://s.luna.tv/search.aspx?client=lunascape&s=0&...
- 8 http://d.hatena.ne.jp/f-star/20081006/p26
- 7 google検索(php switch)
- 7 google検索(php switch)
- 7 google検索(php switch)
- 7 google検索(switch php)
- 7 google検索(php switch )
- 7 google検索(php switch)
- 7 google検索(php switch文)
- 7 google検索(switch php)
- 7 google検索(php switch)
- 7 google検索(php switch文)
- 7 google検索(php switch文)
- 7 google検索(php case文)
- 7 google検索(php switch)
- 6 http://www.revulo.com/
- 6 google検索(php switch)
- 6 google検索(switch php)
- 6 google検索(php switch)
- 6 google検索(php switch文)
- 6 google検索(php switch文)
- 6 google検索(PHP switch)
- 6 google検索(PHP switch)
- 6 google検索(php switch)
- 6 google検索(php switch)
- 6 google検索(php switch)
- 6 google検索(php switch)
