/    Sign up×
Community /Pin to ProfileBookmark

! Is Equal To !== OR !=== ?

Folks,

Are not these exactly the same or will they RETURN a little different results ?

[code]
if(!$fetch_result)
[/code]

[code]
if($fetch_result == FALSE)
[/code]

[code]
if($fetch_result === FALSE)
[/code]

Yes or no ?
I know the difference between the single and dbl and triple equal signs.
I just want to know, is the “!” (first example out of the three above) is a shorthand for the second or third example or not ?

to post a comment
PHP

8 Comments(s)

Copy linkTweet thisAlerts:
@developer_webauthorApr 26.2021 — How do I know what the "!" returns ? Where in php manual this is explained ?

I won't find trouble finding in php manual what following two return:

==
===



Are these valid:

!==

!===

If so, where in php manual they are mentioned ?
Copy linkTweet thisAlerts:
@NogDogApr 26.2021 — !$foo() is the same as $foo() == false, and will match if $foo() returns anything "false-like" (Boolean false, zero, empty string).

$foo() === false will only match if the return value is explicitly a Boolean false (i.e. it won't match if it returns zero or an empty string).
Copy linkTweet thisAlerts:
@developer_webauthorApr 27.2021 — @NogDog#1630894

Sorry! Don't understand this part:

"and will match if $foo() returns anything "false-like" (Boolean false, zero, empty string)."

May I see an example ? Better, two.
Copy linkTweet thisAlerts:
@NogDogApr 27.2021 — When you do a == or != comparison with different [types](https://www.php.net/manual/en/language.types.intro.php) on either side of the operator, PHP tries to convert one or the other thing being compared to the same type as the other. When one of the things is a Boolean (true or false), it will convert the other thing to Boolean. As described in https://www.php.net/manual/en/language.types.boolean.php

> When converting to bool, the following values are considered false:
>
>
  • * the boolean false itself

  • >
  • * the integer 0 (zero)

  • >
  • * the floats 0.0 and -0.0 (zero)

  • >
  • * the empty string, and the string "0"

  • >
  • * an array with zero elements

  • >
  • * the special type NULL (including unset variables)

  • >
  • * SimpleXML objects created from attributeless empty elements, i.e. elements which have neither children nor attributes.

  • >
    > Every other value is considered true (including any resource and NAN).


    If instead you use the === or !== "exact match" operators, no such conversions are done, and the result of the comparison is true only if both things being compared are of the same type as well as being equivalent.
    [code=php]
    if(1 == true) { /* this will execute */ }
    if(1 === true) { /* this will not execute */ }
    [/code]
    Copy linkTweet thisAlerts:
    @developer_webauthorMay 01.2021 — @NogDog#1630954

    **if(1 == true) { /* this will execute */ }**

    And just how will this execute ?
    Copy linkTweet thisAlerts:
    @NogDogMay 01.2021 — > @developer_web#1631079 And just how will this execute ?

    Because == uses loose typing, so the integer 1 will be converted to Boolean true for this comparison (because any integer that is not zero will be treated as true), and thus will be considered equal to the Boolean true.

    If you use the === operator, no loose typing conversions are done, so 1 does not equal true in that case.
    Copy linkTweet thisAlerts:
    @developer_webauthorMay 03.2021 — @NogDog#1631089

    Thanks for reminding what I forgot.

    What about this then ?

    SET 1

    if(0 == ' ') //Space in the quote.

    if(0 == '') //No Space in the quote.

    Which one will execute ? Tell me this without testing. I think you know the answer.

    Without testing, I think last one will execute.

    Now thought up these two on the spot. Are they valid ?

    SET 2

    if(0 < ' ') //Space in the quote.

    if(0 < '') //No Space in the quote.

    Which one will execute ? Tell me this without testing. I think you never experimented with this during your student life. I haven't tested either.

    I know Set 2 has nothing to do with == or ===. But since == and === deal with loose types and non-loose types then I suspect php is more involved in "loose typing" other than the equal sign.
    Copy linkTweet thisAlerts:
    @NogDogMay 03.2021 — See (Comparison with Various Types)[https://www.php.net/manual/en/language.operators.comparison.php#language.operators.comparison.types] for how PHP will do the type conversions.

    After doing that, start thinking about how you will try to avoid such comparisons in you code if possible, or else explicitly setting the types you want to use in your comparisons, e.g.:
    [code=php]
    if(0 < (int) $foo) { /* force integer comparison */ }

    if((str) $foo < (str) $bar) { /* force string comparison */ }
    [/code]
    ×

    Success!

    Help @developer_web spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 3.28,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @darkwebsites540,
    tipped: article
    amount: 10 SATS,

    tipper: @Samric24,
    tipped: article
    amount: 1000 SATS,

    tipper: Anonymous,
    tipped: article
    amount: 10 SATS,
    )...