What is the difference between a!=b and!a==b in PHP?

Source: Internet
Author: User
Tags php operator type null
Sometimes the judgment will find if (!a==b) and if (a!=b) unexpectedly has the difference;
A few times using the previous method did not get the results I want, want to ask the difference between the two.

Add:
Sorry, I wrote it wrong before.
Should there be a difference between a!=b and!a==b?
(Is it not the same as before, because of their own mistakes?!) )

Reply content:

Sometimes the judgment will find if (!a==b) and if (a!=b) unexpectedly has the difference;
A few times using the previous method did not get the results I want, want to ask the difference between the two.

Add:
Sorry, I wrote it wrong before.
Should there be a difference between a!=b and!a==b?
(Is it not the same as before, because of their own mistakes?!) )

Did you write the wrong landlord? Note Yes ! $a == $b , not! $a = $b

Although the precedence of the PHP operator is ! higher than = the priority, it allows a very strange syntax, that is, the assignment will be ! $a = $b given first, $b $a and then the statement will ! $a always return the $b == false same result. and $a != $b not at all comparable.

And ! $a == $b the words should be the $a != $b same ...

Add, according to my understanding ! $a == $b should be and $a != $b the effect is the same, temporarily did not think of a different special case, but there are many values in PHP and Boolen have the same meaning, there are inevitably some in the conversion of the occurrence of the special circumstances you say. Make a list of PHP and false equivalents:

    • The Boolean value FALSE itself

    • Integer value 0 (0)

    • Floating-point value 0.0 (0)

    • An empty string, and the string "0"

    • An array that does not include any elements

    • Objects that do not include any member variables (PHP 4.0 only applies)

    • Special type NULL (including variables that have not been assigned)

    • SimpleXML object generated from an empty tag

For example $a = 0 , $b = 1 then:

    • $a = $bis 1,!1 is false, so if (! $a = $b) is False

    • $a != $bYes 1! = 0 is obviously true

if (! $a = $b) is equivalent to if (! ( $a = $b)).

By the way, which teacher teaches you this kind of writing?

A! = B
!a = b

An equal sign is a value assignment.
Code style is important

// a和b作为字符串, 两者值应该是不相等的; 但是他们对应的布尔值都是false$a = '0';$b = '';$a != $b; // true!$a == $b; // false


The output is as follows

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.