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

后端 未结 15 2197
一向
一向 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条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-11-28 07:22

    Sorry, you cannot use a === comparison in a switch statement, since according to the switch() documentation:

    Note that switch/case does loose comparison.

    This means you'll have to come up with a workaround. From the loose comparisons table, you could make use of the fact that NULL == "0" is false by type casting:

    
    

    Live Demo

提交回复
热议问题