3

As a php neewbie , I try to read a lot of other people´s code in order to learn. Today I came across a line like this :

if ( stripos($post_to_check->post_content, '[' . $shortcode) !== false )

I was wondering what is the difference between !==false and ==true If someone can explain that to me, It would be greatly appreciated. ..and if there is no real difference - what would be the reasons to use the quoted one over the other ??

Obmer kronen
  • 47
  • 1
  • 6
  • http://php.net/manual/en/language.operators.comparison.php – hakre Jan 07 '12 at 10:26
  • 1
    possible duplicate of [Reference - What does this symbol mean in PHP?](http://stackoverflow.com/questions/3737139/reference-what-does-this-symbol-mean-in-php) – hakre Jan 07 '12 at 10:28
  • @hakre not really - it doesn't explain why it's used in this specific case – Pekka Jan 07 '12 at 10:29
  • thanks harke, I have already tried to read this page - but still It is eludes me . I understand the difference between equal and identical. but I can understand that when comparing two variable, and not only ONE to see if it is true or false . or maybe I missed something because of syntay misunderstanding ?? – Obmer kronen Jan 07 '12 at 10:31
  • @Pekka: As there is a difference, the second part of the question does not come to action. So there is no question of use, but only a question of what the difference is ;) - Anyway your answer gives nice info. – hakre Jan 07 '12 at 10:32
  • 1
    @Obmerkronen: I don't understand your comment. What two variables? – hakre Jan 07 '12 at 10:34
  • I am sorry, I will try to better explain my doubt. I can understand the difference between $a !== $b and $a != $b . but the line I wrote, if i am correct (and i might very well be wrong) is checking a string occurrence in another string. it is not comparing two variables. so if there is occurrence it will return true and if no occurrence it will return false. in this specific case , what is the difference between !==false and ==true ? now, like i said, i am quite a newbie - so there can be something else i am missing here.. – Obmer kronen Jan 07 '12 at 10:48

4 Answers4

6

PHP is a loosely typed language. == match the both values and === match the values as well as the data type of values.

if (8 == '8') // returns true

Above condition just match the values not the data type hence if evaluate to TRUE

if (8 === '8') // returns false

and this one check both value and data type of values hence this if evaluate to FALSE

you use === where you want to check the value and data type both and use == when you need to compare only values not the data type.

In your case,

The stripos returns the position of the sub string in the string, if string not found it returns FALSE.

if ( stripos($post_to_check->post_content, '[' . $shortcode) !== false )

The code above check sub string inside string and get evaluate to TRUE only when the sub string found. If you change it to

if ( stripos($post_to_check->post_content, '[' . $shortcode) != false )

and when the sub string found at the 0 position the if evaluate to FALSE even when sub string is there in the main string. Then the condition will become like this

if ( 0 != false )

and this will evaluate to FALSE because the 0 is considered as FALSE

So you have to use there !==

if ( 0 !== false )

This will compare the values and data type of both values The value 0 is an integer type and the false is boolean type, hence the data type does not match here and condition will be TRUE

PHP manual page states these comparison operator you should check this once.

Shakti Singh
  • 84,385
  • 21
  • 134
  • 153
  • Thanks Shakti for your answer, although it did not exactly helped me with the DIRECT doubt I have ( I have asked what is the difference between !==false and ==TRUE , and not between !== false and != false). however, I understood that it all had to do with my neewbie status, and not knowing that the stripos() function is NOT a true/false function. It returns a VALUE or FALSE. in that case, I understand why the specific programmer had used it . I still do not know what the difference between !==false and ==TRUE is .. – Obmer kronen Jan 08 '12 at 03:35
3

The difference between !==false and ==true is the difference between an identical/not-identical and equal/non-equal comparison in PHP.

Please see the Comparison Operators in the PHP Manual what the difference between Identical and Equal is.

hakre
  • 193,403
  • 52
  • 435
  • 836
  • thanks harke for your answer, like said before, I have already read that page . What I was ignorant about was the fact that stripos() is NOT a true/false function, it is a value/false function. If it were a true/false function, I still have a problem understanding the difference between !==false and ==true (unless "true" and "false" can be a string ?? ) – Obmer kronen Jan 08 '12 at 03:39
  • `==` = equality comparison. `===` = identical comparison. `!==` is the negation of the identical comparison, not the negation of the equality comparison. maybe that's the point? – hakre Jan 08 '12 at 10:57
  • Like I said, I understand the equality != Identical :-) anyway, I can not thank enough all of you for answering, I have learned a lot , even if by the looks of ups and downs in this question - even experienced users have their doubts about this one ... thanks again , – Obmer kronen Jan 08 '12 at 11:41
1

!== is a comparison that doesn't only compare the value, but also the type of both variables.

It is used here because stripos can return false when no hit was found, but also 0 when a hit was found in the first character of the string.

== is unable to distinguish those two cases (they are both "falsy"), so you have to use === when working with stripos. There's a warning in the manual:

This function may return Boolean FALSE, but may also return a non-Boolean value which evaluates to FALSE, such as 0 or "". Please read the section on Booleans for more information. Use the === operator for testing the return value of this function.

hakre
  • 193,403
  • 52
  • 435
  • 836
Pekka
  • 442,112
  • 142
  • 972
  • 1,088
-1

Note: ==true and ===true are different.

I think !==false is similar to ===true, so only explain ==true and ===true. For the first ==, it is equal in value, thus 1 == true, 0==false. for ===, it is "identical" in PHP, namely, equal in value, and also in type.

thus, if the result is at 0th position, result should be true; however, if use ==true, it will not work as 0!= true.

For example, stripos('a sheep', 'a') if you use ==true, the result is wrong as it is at first place.

Alex
  • 370
  • 1
  • 6
  • 11
  • Why the hell is everybody downvoting perfectly valid answers here like this? It's a good point and helpful! Seriously, I feel the quality of StackOverflow is starting to go the way of Wikipedia, as it is gaining higher and higher exposure... – P Varga Jan 07 '12 at 11:54