Jump to content
Larry Ullman's Book Forums

Recommended Posts

 

I have a feeling I've seen such triple operators (!==, === etc.) in the code before, but I can't quite remember what they mean.

 

On Pg. 404 (at the very bottom of it), this operator is used in the following bit of code:

 


foreach ($very_bad as $v) {
if (stripos($value, $v) !== false) return '';
}


 

I don't think this has ever been explained in the book up to this point, unless I somehow missed such explanation.

 

Could someone knowledgeable educate me, please?

Link to comment
Share on other sites

I would bet it is explained. This is really information a quick google search would give you thought.

 

Tripple comparison performs check on type as well as normal comparison.

 

Notice the HUGE warning on the documentation on stripos? That tells you that this function is not binary safe. This basically means that you must look out for values that will equate to boolean false. The most obvious (and seriously stupid) example is "", an empty string.

 

The problem arises because this function returns boolean false on errors. There are in other words no way to know if an error happend or you got an empty String. The very simple solution is to also check for type.

 

// Try getting the index of an empty String from array holding empty String (Result is 0)
$result = stripos(array(""), "");

// Perform normal comparison (Result is "Error")
echo ( $result != false ) ? "..." : "Error";

// Perform type comparison (Result is "Works")
echo ( $result !== false ) ? "Works. Found at index: {$result}" : "...";

  • Upvote 1
Link to comment
Share on other sites

Hmm... Let me get this straight.

 

So, stripos() function may return either some result, or false.

 

We want to make sure it's not false. What's wrong with plain "!= false"? That would be making sure the result is not false, correct?

 

Why would we need !== false? To check if the result is not boolean false? Is there any other type of false?

 

I probably somehow misunderstood your answer, so please forgive me for not letting this go and asking questions that may be a little basic. (I'm a musician by education, not a programmer. ;-)

Link to comment
Share on other sites

Yes, there are different types of false. Quoting the php.net article linked below, all of the following values "loosely" evaluate to false:

 

When converting to boolean, the following values are considered FALSE:

- the boolean FALSE itself

- the integer 0 (zero)

- the float 0.0 (zero)

- the empty string, and the string "0"

- an array with zero elements

- an object with zero member variables (PHP 4 only)

- the special type NULL (including unset variables)

- SimpleXML objects created from empty tags

(Source: http://php.net/manual/en/language.types.boolean.php)

 

If you want to create a quick test, run the following scripts from your localhost:

 

<?php

 if (0 != false) {

   echo 123;

 } else {

   echo 456;

 }

 

The above will echo 456 because 0 "loosely" evaluates to false. However, the following script will echo 123 because 0 does not "strictly" evaluate to false (in other words, the integer 0 and the Boolean "false" are indeed different values, as you stated).

 

 

<?php

 if (0 !== false) {

   echo 123;

 } else {

   echo 456;

 }

 

Given the above, if the substring you're searching for just happened to start at the first character of the string (in other words, index 0), then you are not going to get what you want using !=, but you will get what you want when you use !==.

 

That make sense?

  • Upvote 2
Link to comment
Share on other sites

Just to tie in with Antonio's explanation a bit more, == and != only compare two values, not types. As such 0 == false evaluates to true.

However, === and !== compare both values and types, and since 0 is an integer and false is a Boolean, 0 === false evaluates to false.

Link to comment
Share on other sites

Thank you, HartleySan, for such a thorough and detailed explanation.

 

There's one thing I still need clarification with, though, and would very much appreciate it.

 

If I'm understanding this correctly, stripos() function returns either a result, or false.

 

The code on page 404 uses stripos() function:

 

 

foreach ($very_bad as $v) {
if (stripos($value, $v) !== false) return '';
}

 

So if it's false, which is boolean, why would we need "!=="?

 

I mean, it doesn't return 0, or 0.0, or "0" or "", or anything like that. I just returns plain old boolean false, correct? So why do we still need !== in that case?

Link to comment
Share on other sites

It could return 0. It will do that in my example, as an empty string is in position 0 in the array. (first and only element) As 0 equates to false with != (because of type cast) it would SEEM like an error occurred even though it should not.

 

That is why I used the example I did. You should try running the code. I even provided you the output of each comparison.

 

What you should learn from this: Use the value and type comparison operator (===) whenever a function is marked "not binary safe" in the manual, or whenever you NEED to enforce type.

 

I NEVER use normal value checks anymore unless I have to. Too much potensial for fuck-ups in the code, really. That operator is the reason for many a bug found in PHP, and the use of it should die...

  • Upvote 2
Link to comment
Share on other sites

 Share

×
×
  • Create New...