PHP in_array() / array_search() odd behaviour

前端 未结 2 1792
栀梦
栀梦 2020-11-22 08:05

I have found some odd behaviour while I was using the PHP function in_array(). I have an array like this:

$arr = [TRUE, \"some string\", \"somet         


        
2条回答
  •  攒了一身酷
    2020-11-22 08:10

    You are right, the boolean can indeed cause this. Set the strict flag in the in_array function, this way also the type of the element is checked (basically the same as using ===):

    if (in_array("username", $results, true)) // do something
    if (in_array("password", $results, true)) // do something
    if (in_array("birthday", $results, true)) // do something
    

提交回复
热议问题