What does an exclamation mark before a function really mean in PHP

Just as in the title, what does an exclamation mark before a function really mean in PHP?

For example is the following statement:

if (!stripos($haystack, $needle)) {}

the same as this:

if (stripos($haystack, $needle) === FALSE) {}

or this:

if (stripos($haystack, $needle) == FALSE) {}

Any clarification would be appreciated

Here is Solutions:

We have many solutions to this problem, But we recommend you to use the first solution because it is tested & true solution that will 100% work for you.

Solution 1

The ! preceding the function is the same as…

if (stripos($haystack, $needle) == FALSE) {}

It’s the same because it is a == comparison which doesn’t check types.

It’s called the negation unary operator. It flips the Boolean value (coercing to Boolean if need be) of a value.

For example…

! 0;    // True
! 1 ;   // False
! '';   // True
! true; // False
!! 0    // False 

The !! trick is handy in languages without a (bool) cast. By flipping a value twice, you get the Boolean version of its original value.

Solution 2

! will work on value comparison so it’s same as the second comparison I think. because ! will catch false, 0, null as false, and they are not the same types as you see. “” may be an exception for that because I always use trim()!=”” to compare the strings. I don’t know about that.

Note: Use and implement solution 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply