5
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

ifやswitchを使って場合分けをすると、バグを埋め込みやすくなるので最近は避けがちなのですが、たまーに使いたくなるときがあります。簡単なコードを書くときとか。
phpのswitchは、緩やかな比較を行っているようで、ちょっと意図しない結果になることがあるようです。

参考:PHP 型の比較表

function switchTest($switchCase) {
    switch ($switchCase) {
        case 'ok':
            return 'ok';

        case 'ng':
            return 'ng';

        case true:
            return 'true';

        case false:
            return 'false';

        default:
            return 'default';
    }
}

var_dump(switchTest(true)); // ok
var_dump(switchTest(false)); // false
var_dump(switchTest('ng')); // ng
var_dump(switchTest(0)); // ok

まぁ、あまり無茶なswitchを使わないほうが良いなと思いました。

5
6
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?