PHP の switch 文は判定が緩やか

PHP の switch 文は怖くて使えない – れぶろぐ (2008-10-02)

[php]
function filter($value) {
switch ($value) {
case ‘abc’:
case ‘def’:
return $value;
break;
default:
return ”;
break;
}
}
[/php]

といったプログラムの場合、0を与えたら”"が戻ってきてほしいものですが、

[php]
php > echo filter(0);
0
[/php]

こうなってしまうらしい。。うーむ。確かにマニュアルコメントにも怪しそうな挙動が載ってますねえ。

[php]

$string="2string";

switch($string)
{
case 1:
echo "this is 1";
break;
case 2:
echo "this is 2";
break;
case '2string':
echo "this is a string";
break;
}

?>
[/php]

The swich-statement will halt on ‘case 2′

Answer: this is 2

とか。うーむ。気をつけましょう。

関連する記事:

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="">