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
  • 3
    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
  • 1
    Didn’t know that returning values of different types is considered normal in dynamically typed languages and is even present in the standard lib.

    I’m horrified even thinking about coding in such languages.
  • 1
    @Lensflare hahaha it’s still a type, a “mixed” type
  • 1
    @dUcKtYpEd Is that how they are called? Mixed types? I didn’t find anything useful googling it.
  • 1
    @Lensflare pho annotations cal them mixed types. It’s just the way it is. Return what you want but be responsible and return responsibily. Only return mixed types of absolutely necessary
Add Comment