LoginSignup
1
0

More than 3 years have passed since last update.

PHP swithc文で型比較に対応する

Last updated at Posted at 2020-12-08

目的

  • PHPのswitch文で詰まった部分を簡単にまとめる

前提情報

詳細

  • 下記のように記載することで$fooにstringの1かintの1かを厳密に比較する事ができる。

    <?php
    
    switch (true) {
        case $foo === 1:
            echo 'int';
            break;
        case $foo === '1':
            echo 'string';
            break;
    }
    ?>
    
  • $fooにintの1が格納されている時には「int」と表示され、stringの1が格納されているときには「string」と表示される。

1
0
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
1
0