Home Backend Development PHP Tutorial Simple comparison of equation (==) and identity (===) operators in PHP

Simple comparison of equation (==) and identity (===) operators in PHP

Feb 01, 2019 am 10:33 AM
== === php comparison operator

Equality (==) and identity (===) are both PHP comparison operators, so what are the differences between them? The following article will give you a brief comparison between equation (==) and identity (===). I hope it will be helpful to you. [Tutorial recommendation: PHP tutorial]

Simple comparison of equation (==) and identity (===) operators in PHP

##Equality (==) operator

The equality (==) operator compares and tests whether the variable (expression or constant) on the left has the same value as the variable (expression or constant) on the right; the comparison performed by this operator is loose of.

If the two values ​​​​are the same (it only compares the value of the variable, not the data type), it returns a true value; if the two values ​​​​are not the same, it returns a false value.

Note: The equality (==) operator and the assignment (=) operator are different. The assignment (=) operator changes the variable on the left, assigning the variable on the right to the variable on the left, while the equality (==) operator tests for equality and returns true or false depending on the comparison.

Example:

<?php 
header("content-type:text/html;charset=utf-8");  
// 给变量赋整数值
$x = 999; 
echo &#39;$x=&#39;.$x."<br>"; 
// 给变量赋字符串值
$y = &#39;999&#39;; 
echo &#39;$y=&#39;.$y."<br>"; 
//比较$x 和$y 
if ($x == $y) 
    echo &#39;$x和$y的值相等&#39;; 
else
    echo &#39;$x和$y的值不相等&#39;; 
?>
Copy after login

Output:

Simple comparison of equation (==) and identity (===) operators in PHP

Instructions:In the above example , because the equality (==) operator only compares the values ​​of variables, the values ​​​​of $x and $y are equal, so the statement in the if is directly executed, and the else statement is not executed.

Comparison chart of PHP equation (==) operators:

Simple comparison of equation (==) and identity (===) operators in PHP

Equality (== =) Operator

The identity (===) operator performs a strict comparison between given variables or values; it compares and looks at two variables (expressions or constants ) if the values ​​are equal and have the same data type, i.e. both are strings or both are integers etc.

This operator returns true if two variables (expressions or constants) contain the same value and the same data type, otherwise it returns false.

Example:

<?php 
header("content-type:text/html;charset=utf-8");  
// 给变量赋整数值
$x = 999; 
echo &#39;$x=&#39;.$x."<br>"; 
// 给变量赋字符串值
$y = &#39;999&#39;; 
echo &#39;$y=&#39;.$y."<br>"; 
//比较$x 和$y 
if ($x === $y) 
    echo &#39;$x和$y相等&#39;; 
else
    echo &#39;$x和$y不相等&#39;; 
?>
Copy after login

Output:


Simple comparison of equation (==) and identity (===) operators in PHP

Explanation:at In the above example, the values ​​of $x and $y are equal but the data types are different, so false is returned and the else part is executed.

Comparison chart of PHP identity (===) operators:

Simple comparison of equation (==) and identity (===) operators in PHP

The above is the entire content of this article, I hope It can be helpful to everyone’s study. For more exciting content, you can pay attention to the relevant tutorial columns of the PHP Chinese website! ! !

The above is the detailed content of Simple comparison of equation (==) and identity (===) operators in PHP. For more information, please follow other related articles on the PHP Chinese website!

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

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

CakePHP Date and Time

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

CakePHP File upload

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

CakePHP Routing

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

Discuss CakePHP

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

CakePHP Project Configuration

CakePHP Quick Guide CakePHP Quick Guide Sep 10, 2024 pm 05:27 PM

CakePHP Quick Guide

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

How To Set Up Visual Studio Code (VS Code) for PHP Development

See all articles