Home > Backend Development > PHP Tutorial > What\'s the Difference Between =, ==, and === in PHP?

What\'s the Difference Between =, ==, and === in PHP?

Susan Sarandon
Release: 2024-10-29 08:03:30
Original
994 people have browsed it

 What's the Difference Between =, ==, and === in PHP?

Understanding the Subtleties of =, ==, and === in PHP

In PHP, the use of =, ==, and === for comparisons often raises questions. Let's delve into the distinctions between these operators and their appropriate applications.

Assignment Operator =

= is an assignment operator. It assigns the value of the right-hand side (operand) to the left-hand side (variable):

<code class="php">$a = 10;  // Assigns the value 10 to variable $a</code>
Copy after login

'Equal' Comparison Operator ==

== is an 'equal' comparison operator. It evaluates if the values of both operands are equal, regardless of their types:

<code class="php">$a == 10;  // True if $a is equal to 10 (even if $a is a string)</code>
Copy after login

'Identical' Comparison Operator ===

=== is an 'identical' comparison operator. It goes beyond value equality and ensures that the operands are not only equal in value but also identical in data type:

<code class="php">$a === 10;  // True if $a is both equal to 10 and an integer</code>
Copy after login

Summary Table

Operator Description
= Assigns the value of the right-hand side to the left-hand side
== Compares the values of both operands for equality, regardless of type
=== Compares the values and data types of both operands for identity

The above is the detailed content of What\'s the Difference Between =, ==, and === in PHP?. For more information, please follow other related articles on the PHP Chinese website!

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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template