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

后端 未结 15 2127
一向
一向 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:13

    Nope. From the manual page:

    Note that switch/case does loose comparison.

    If you only have two conditions, use an if like your second example. Otherwise, check for NULL first and switch on the other possibilities:

    if (is_null($var))
    {
      return 'a';
    }
    
    switch ($var)
    {
        // ...
    }
    

提交回复
热议问题