php has some of the worst quirks.

For instance:
array_search(needle, haystack) returns the key thats found, otherwise false. It returns false so we can determine if it was found in a conditional obviously.

So what if the key found is 0 ......

  • 3
    in PHP, 0!==FALSE.
    So you can just use if(array_search(...)===FALSE) // not found
  • 1
    @ZioCain so I can’t if(array_searc()) but if(array_searcg() !==False) is fine?
  • 1
    @dUcKtYpEd yeah, bc if you don't specify === then it will automatically use ==true
  • 2
    PHP is a minefield. 🤷🏻‍♀️
    Know your tools.

    And, if necessary, pick better ones.
  • 1
    @ZioCain You *could* do that, but when you forget to, the return value of false conveniently gets converted to 0 when used as an index without any warning

    And yes I know that C has the same problem but with -1 instead of false
  • 1
    That’s what I mean it’s just quirky and like @root is saying, a mine that in this instance, caught me horribly. I can work with -1 a lot better then I can 0
Add Comment