make switch use === comparison not == comparison In PHP

后端 未结 15 2158
一向
一向 2020-11-28 07:09

Is there anyway to make it so that the following code still uses a switch and returns b not a? Thanks!

$var = 0;
switch($var) {
            


        
15条回答
  •  一向
    一向 (楼主)
    2020-11-28 07:27

    I just use

    $var === null and $var = -1; // since switch is not type-safe
    switch ( $var ) {
        case 0:
            # this tests for zero/empty string/false
            break;
        case -1:
            # this tests for null
            break;
    }
    

    I think this still looks very readable if the comment starting with // is left behind (and the ones starting with # are probably best deleted).

提交回复
热议问题