Home > Backend Development > PHP Tutorial > The difference in usage between congruent === and equal == in php

The difference in usage between congruent === and equal == in php

WBOY
Release: 2016-07-28 08:29:38
Original
997 people have browsed it

The difference between the usage of congruence (===) and equality (==) in php

Let’s take a look at the following program: $str = “0d52”; If (0==$str) {echo “true”} Else {echo “false”}

The result of running this program is unexpected. “0d52” is actually considered equal to 0 by PHP. Why is there such a situation? When performing the relational operation "==", the data types on both sides of the operator must be consistent, so the string on the right side of the equal sign is forced to be converted to integer type 0.

This is the shortcoming of many weakly typed languages. This kind of error cannot be tolerated in our program. Is there any way to solve this problem? The answer is of course yes, PHP provides us with equals to solve similar problems.

Now we rewrite the program into the following form to explain the working principle of equal to . $str = “0d52”; If (0===$str) {echo “true”} Else {echo “false”}

The equal operation process is as follows: 1. Determine whether the data type of the two sides of the equal operator is If they are the same, return false 2. Determine whether the values ​​of the two sides of the equal operator are equal. If they are not equal, return false 3. Finally, perform the AND operation of the above 2 steps. Returns the result of the AND operation.

The operation process of not equal to equal is exactly the opposite of that of equal to equal: 1. Determine whether the data types of the two sides of the not equal to equal operator are the same. If they are not the same, return true. 2. Judge whether the values ​​of the two sides of the not equal to equal to operator are equal. If they are not equal, , then return true 3. Finally, perform or operate the above 2 steps. Returns the result of an OR operation.

Okay, I have finished explaining why congruence in php is not equal, I hope readers who are php enthusiasts will benefit from it!

The above introduces the usage differences between congruent === and equal == in PHP, including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template