In PHP, 0 (int, zero) is equal to “first” or “last” (strings)?

前端 未结 4 784
猫巷女王i
猫巷女王i 2021-01-21 07:43

I\'m confused by something I just ran into in a script I was working on. I had the following:

function getPart($part)
{
    $array = array(\'a\', \'b\', \'c\');         


        
4条回答
  •  青春惊慌失措
    2021-01-21 08:24

    PHP is weakly typed. What's happening there is it's trying to convert "first" to a number. It fails, and returns zero. It now has two numbers: zero and zero. To make it not try to convert the types, use === rather than ==.

提交回复
热议问题