PHP and the great „===“ operator

Lets suppose you’ve got an array with numeric indices:

[0] => ‚bla‘
[1] => ‚blub‘

Now you want to do something if the element ‚bla‘ is found in that array.
Well, you know that PHP has a function array_search, which returns the key(s) of the found values. Lets say, you write something like this:

if(array_search($array, ‚bla‘)) { do_something }

Would you expect that do_something would actually do something?
If yes: You are wrong.
If no: Great, you’ve understand some parts of PHP – insanitygoodness..

Actually, if ‚bla‘ wouldn’t have the index 0 it would work, because 1, 2, 3, 4, etc. is TRUE. But unfortunately PHP has some sort of implicit casting, which makes 0 behave like a FALSE, depending on the context. So following this, the if() works for all elements except 0.

You might be tempted to write

if(array_search… != FALSE)

But this wouldn’t help you, because 0 would still evaluate to FALSE, leading to if(FALSE != FALSE) which is (hopefully obviously) never true.

A PHP beginner (or even an intermediate, if he never stumbled across this case) might ask:
Whats the solution for this dilemma?

Luckily the PHP documentation is great. It tells you about this. And additionally PHP has got this great operator (===), which causes people to ask „WTF?“ when they hear about it for the first time. Additionaly to comparing the values to each other, they also check the type of the variable. This leads to the wanted result because 0 is an integer, while FALSE is Boolean. So the solution for our problem looks like this:

if (array_search($array, ‚bar‘) !== FALSE) {

Isn’t this great?

15 Gedanken zu „PHP and the great „===“ operator“

  1. I like to bash PHP as much as the next guy, but in this case the correct solution would be to simply use in_array(), which does exactly what you want.

  2. toupeira,
    yes, you are right. I must confess I didn't look properly if such a function exists. Still, using array_search would be a an alternative way of doing the job (although not the best) and it would work almost intuitively if this ambiguity wouldn't exist. So consider that post a flame about the ambiguity, not the specific use case.

  3. I agree, the real problem is that PHP doesn't offer an obvious way to do this 😉 For example Python has the "in" operator ('bla' in array), or Ruby has a "include?" method (array.include? 'bla').

    By the way, I just realized that you've also got the argument order wrong, it should be array_search('bar', $array). Another one of those things PHP really sucks at 😉

  4. Indeed, I regulary make this mistake with the order, but thats my fault. From a logic point of view _in this case_ this isn't that bad. This way the whole function call can be seen as "search for x in y"

  5. Why no one writes : Python and the great 'is' operator?

    Is it because the is operator is well known?

    In [1]: False != 0
    Out[1]: False

    In [2]: False is not 0
    Out[2]: True

  6. Abderrahim,

    you have a good point or would have one if my post would have compared PHP to Python. Yes, python has a similar behaviour, because 'is' checks for object identity not for value identity.
    Personally I however never stumbled across that. Eventually because I knew 'if x is in y' from the very beginning, when I learned python, because every single howto/book/tutorial explains it when explaining lists. Eventually, because I learned python different as I learned PHP. Eventually because working with the basic data types in Python seems more tense, then working with the basic data types in PHP. I dunno.

    But that alone isn't the point. Whereas python has choosen to use a quiet obvious form to describe those relations (is to describe weither a is b is actually clear isn't it?), while PHP decided to overload the meaning of an existing operator for equality (!) operator. What does === stand for? equals equal? equals equal sign?

  7. jsr solutions a software development company is organising a test in quest univercity Mohali(landra) on the basis of apttitude and gernal knowledge.The top 50 students who pass this test have to be taken for you can register your roll no here B-tech Training on free

  8. IM is already there and with jabber really well done, why not define several additional personal information meta data and assign external urls/services what not to it, so you can browse your buddys and show all the information lotro gold from several different sources if you like (maybe even in a nice context based Mabinogi Gold plasmoid view). You could even get the relevant information Maple story mesos from other services (local and private ones) hidden from the IM service, but only mapped by the client to your addressbook.

    so .. lots Wow Power Leveling of good things, but the whole “it sucks because our graphics Rom Gold layer Ragnarok Zeny sucks” thing has really got me by the balls.

    When all the feathers are gone, when Buy Aion Gold the blue sky,aion power leveling we, white clouds, when grasslands are gone, Ling Po stood up,buy aion gold etc., his heart to absorb the magic Shiratori finished after the ready to set off for the fifth floor, just as the blue sky, white clouds of regret has been disappearing grassland ,buy aion gold making, and innate goodness was his burial. Yes, he has Aion Power Leveling figured out that several war down, all in all, just as it sounds exactly the same as if he did not kill those monsters, then Dofus Kamas this time he does not have the opportunity to stand up again .

  9. You would use array_search if you want to check if a value exists AND wish to return the index of that value. Nothing wrong with using it in this case.

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert.