What is the difference between == and === in php [duplicate]

Asked
Active3 hr before
Viewed126 times

6 Answers

differenceduplicate
90%

Difference between == (equal) and === (identical equal),PHP provides two comparison operators to check equality of two values. The main difference between of these two is that '==' checks if the values of the two operands are equal or not. On the other hand, '===' checks the values as well as the type of operands are equal or not.,The difference between the loosely == equal operator and the strict === identical operator is exactly explained in the manual:,Warning: two instances of the same class with equivalent members do NOT match the === operator. Example:

Comparison Operators

┌──────────┬───────────┬───────────────────────────────────────────────────────────┐│
Example│ Name│ Result│├──────────┼───────────┼───────────────────────────────────────────────────────────┤│ $a == $b│ Equal│ TRUE
if $a is equal to $b after type juggling.││$a === $b│ Identical│ TRUE
if $a is equal to $b, and they are of the same type.│└──────────┴───────────┴───────────────────────────────────────────────────────────┘
load more v
88%

Is there any difference between == and === in php? both seem to work fine for me when i use them in a conditional statement. I am very new to programming in PHP. Please consider this and answer in simple words.,Read here for more: http://www.php.net/manual/en/language.operators.comparison.php,TRUE if $a is equal to $b, and they are of the same type. (introduced in PHP 4),Identical true: if $a is equal to $b, and they are of the same type.

load more v
72%

1. = : "=" stands for assignment operator. This operator is used for assigning values to variables.,3. '===' : "===" stands for identical operator. This operator checks the values as well as the type of operands are same or not.,2. == : "==" stands for equality operator. This operator used to check the value of operands are equal or not.,Note : '==(equal)' operator performs typecasting automatically to compare values of operand while '===(identical)' operator performs typesafe comparison.

Example :

< ? php
$var = 'Test'; ?
>
load more v
65%

For the difference of === and ==, read PHP type comparison tables,In PHP, is there any difference between the != and <> operators?,== and != do not take into account the data type of the variables you compare. So these would all return true:,In the main Zend implementation there is not any difference. You can get it from the Flex description of the PHP language scanner:

In the manual, it states:

$a != $b Not equal TRUE
if $a is not equal to $b after type juggling.
$a < > $b Not equal TRUE
if $a is not equal to $b after type juggling.
load more v
75%

Difference Between the == and the === Comparison Operators in PHP,Compare an Integer Value and a Boolean Value With the Double (==) and Triple (===) Equals Comparision Operator in PHP,Compare an Integer Value and a Floating Point Value With the Double (==) and Triple (===) Equals Comparision Operator in PHP,Compare an Integer Value and a String Value With the Double and Triple Equals Comparision Operator

Example Code:

#php 7. x
   <
   ? php
$a = 5;
$b = 5.0;
$c = $a === $b ? true : false;
$d = $a == $b ? true : false;
var_dump($c);
var_dump($d); ?
>
load more v
40%

Before utilizing these tables, it's important to understand types and their meanings. For example, "42" is a string while 42 is an int. false is a bool while "false" is a string. , Simply doing if ($x) while $x is undefined will generate an error of level E_NOTICE. Instead, consider using empty() or isset() and/or initialize your variables. , The following tables demonstrate behaviors of PHP types and comparison operators, for both loose and strict comparisons. This supplemental is also related to the manual section on type juggling. Inspiration was provided by various user comments and by the work over at » BlueShoes. , HTML Forms do not pass integers, floats, or booleans; they pass strings. To find out if a string is numeric, you may use is_numeric().

The following tables demonstrate behaviors of PHP types and comparison operators, for both loose and strict comparisons. This supplemental is also related to the manual section on type juggling. Inspiration was provided by various user comments and by the work over at » BlueShoes.

Before utilizing these tables, it's important to understand types and their meanings. For example, "42" is a string while 42 is an int. false is a bool while "false" is a string.

"42"

The following tables demonstrate behaviors of PHP types and comparison operators, for both loose and strict comparisons. This supplemental is also related to the manual section on type juggling. Inspiration was provided by various user comments and by the work over at » BlueShoes.

Before utilizing these tables, it's important to understand types and their meanings. For example, "42" is a string while 42 is an int. false is a bool while "false" is a string.

42

The following tables demonstrate behaviors of PHP types and comparison operators, for both loose and strict comparisons. This supplemental is also related to the manual section on type juggling. Inspiration was provided by various user comments and by the work over at » BlueShoes.

Before utilizing these tables, it's important to understand types and their meanings. For example, "42" is a string while 42 is an int. false is a bool while "false" is a string.

"false"
load more v

Other "difference-duplicate" queries related to "What is the difference between == and === in php [duplicate]"