Viewed   147 times

What is the difference between == and ===?

  • How exactly does the loosely == comparison work?
  • How exactly does the strict === comparison work?

What would be some useful examples?

 Answers

2

Difference between == and ===

The difference between the loosely == equal operator and the strict === identical operator is exactly explained in the manual:

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. ?
????????????????????????????????????????????????????????????????????????????????????

Loosely == equal comparison

If you are using the == operator, or any other comparison operator which uses loosely comparison such as !=, <> or ==, you always have to look at the context to see what, where and why something gets converted to understand what is going on.

Converting rules

  • Converting to boolean
  • Converting to integer
  • Converting to float
  • Converting to string
  • Converting to array
  • Converting to object
  • Converting to resource
  • Converting to NULL

Type comparison table

As reference and example you can see the comparison table in the manual:

Loose comparisons with ==

?????????????????????????????????????????????????????????????????????????????????????????????????????????????
?         ? TRUE  ? FALSE ?   1   ?   0   ?  -1   ?  "1"  ?  "0"  ? "-1"  ? NULL  ? array() ? "php" ?  ""   ?
?????????????????????????????????????????????????????????????????????????????????????????????????????????????
? TRUE    ? TRUE  ? FALSE ? TRUE  ? FALSE ? TRUE  ? TRUE  ? FALSE ? TRUE  ? FALSE ? FALSE   ? TRUE  ? FALSE ?
? FALSE   ? FALSE ? TRUE  ? FALSE ? TRUE  ? FALSE ? FALSE ? TRUE  ? FALSE ? TRUE  ? TRUE    ? FALSE ? TRUE  ?
? 1       ? TRUE  ? FALSE ? TRUE  ? FALSE ? FALSE ? TRUE  ? FALSE ? FALSE ? FALSE ? FALSE   ? FALSE ? FALSE ?
? 0       ? FALSE ? TRUE  ? FALSE ? TRUE  ? FALSE ? FALSE ? TRUE  ? FALSE ? TRUE  ? FALSE   ? TRUE  ? TRUE  ?
? -1      ? TRUE  ? FALSE ? FALSE ? FALSE ? TRUE  ? FALSE ? FALSE ? TRUE  ? FALSE ? FALSE   ? FALSE ? FALSE ?
? "1"     ? TRUE  ? FALSE ? TRUE  ? FALSE ? FALSE ? TRUE  ? FALSE ? FALSE ? FALSE ? FALSE   ? FALSE ? FALSE ?
? "0"     ? FALSE ? TRUE  ? FALSE ? TRUE  ? FALSE ? FALSE ? TRUE  ? FALSE ? FALSE ? FALSE   ? FALSE ? FALSE ?
? "-1"    ? TRUE  ? FALSE ? FALSE ? FALSE ? TRUE  ? FALSE ? FALSE ? TRUE  ? FALSE ? FALSE   ? FALSE ? FALSE ?
? NULL    ? FALSE ? TRUE  ? FALSE ? TRUE  ? FALSE ? FALSE ? FALSE ? FALSE ? TRUE  ? TRUE    ? FALSE ? TRUE  ?
? array() ? FALSE ? TRUE  ? FALSE ? FALSE ? FALSE ? FALSE ? FALSE ? FALSE ? TRUE  ? TRUE    ? FALSE ? FALSE ?
? "php"   ? TRUE  ? FALSE ? FALSE ? TRUE  ? FALSE ? FALSE ? FALSE ? FALSE ? FALSE ? FALSE   ? TRUE  ? FALSE ?
? ""      ? FALSE ? TRUE  ? FALSE ? TRUE  ? FALSE ? FALSE ? FALSE ? FALSE ? TRUE  ? FALSE   ? FALSE ? TRUE  ?
?????????????????????????????????????????????????????????????????????????????????????????????????????????????

Strict === identical comparison

If you are using the === operator, or any other comparison operator which uses strict comparison such as !== or ===, then you can always be sure that the types won't magically change, because there will be no converting going on. So with strict comparison the type and value have to be the same, not only the value.

Type comparison table

As reference and example you can see the comparison table in the manual:

Strict comparisons with ===

?????????????????????????????????????????????????????????????????????????????????????????????????????????????
?         ? TRUE  ? FALSE ?   1   ?   0   ?  -1   ?  "1"  ?  "0"  ? "-1"  ? NULL  ? array() ? "php" ?  ""   ?
?????????????????????????????????????????????????????????????????????????????????????????????????????????????
? TRUE    ? TRUE  ? FALSE ? FALSE ? FALSE ? FALSE ? FALSE ? FALSE ? FALSE ? FALSE ? FALSE   ? FALSE ? FALSE ?
? FALSE   ? FALSE ? TRUE  ? FALSE ? FALSE ? FALSE ? FALSE ? FALSE ? FALSE ? FALSE ? FALSE   ? FALSE ? FALSE ?
? 1       ? FALSE ? FALSE ? TRUE  ? FALSE ? FALSE ? FALSE ? FALSE ? FALSE ? FALSE ? FALSE   ? FALSE ? FALSE ?
? 0       ? FALSE ? FALSE ? FALSE ? TRUE  ? FALSE ? FALSE ? FALSE ? FALSE ? FALSE ? FALSE   ? FALSE ? FALSE ?
? -1      ? FALSE ? FALSE ? FALSE ? FALSE ? TRUE  ? FALSE ? FALSE ? FALSE ? FALSE ? FALSE   ? FALSE ? FALSE ?
? "1"     ? FALSE ? FALSE ? FALSE ? FALSE ? FALSE ? TRUE  ? FALSE ? FALSE ? FALSE ? FALSE   ? FALSE ? FALSE ?
? "0"     ? FALSE ? FALSE ? FALSE ? FALSE ? FALSE ? FALSE ? TRUE  ? FALSE ? FALSE ? FALSE   ? FALSE ? FALSE ?
? "-1"    ? FALSE ? FALSE ? FALSE ? FALSE ? FALSE ? FALSE ? FALSE ? TRUE  ? FALSE ? FALSE   ? FALSE ? FALSE ?
? NULL    ? FALSE ? FALSE ? FALSE ? FALSE ? FALSE ? FALSE ? FALSE ? FALSE ? TRUE  ? FALSE   ? FALSE ? FALSE ?
? array() ? FALSE ? FALSE ? FALSE ? FALSE ? FALSE ? FALSE ? FALSE ? FALSE ? FALSE ? TRUE    ? FALSE ? FALSE ?
? "php"   ? FALSE ? FALSE ? FALSE ? FALSE ? FALSE ? FALSE ? FALSE ? FALSE ? FALSE ? FALSE   ? TRUE  ? FALSE ?
? ""      ? FALSE ? FALSE ? FALSE ? FALSE ? FALSE ? FALSE ? FALSE ? FALSE ? FALSE ? FALSE   ? FALSE ? TRUE  ?
?????????????????????????????????????????????????????????????????????????????????????????????????????????????
Thursday, September 1, 2022
5

Here's what worked best for me when trying to script this (in case anyone else comes across this like I did):

$ pecl -d php_suffix=5.6 install <package>
$ pecl uninstall -r <package>

$ pecl -d php_suffix=7.0 install <package>
$ pecl uninstall -r <package>

$ pecl -d php_suffix=7.1 install <package>
$ pecl uninstall -r <package>

The -d php_suffix=<version> piece allows you to set config values at run time vs pre-setting them with pecl config-set. The uninstall -r bit does not actually uninstall it (from the docs):

vagrant@homestead:~$ pecl help uninstall
pecl uninstall [options] [channel/]<package> ...
Uninstalls one or more PEAR packages.  More than one package may be
specified at once.  Prefix with channel name to uninstall from a
channel not in your default channel (pecl.php.net)

Options:
  ...
  -r, --register-only
        do not remove files, only register the packages as not installed
  ...

The uninstall line is necessary otherwise installing it will remove any previously installed version, even if it was for a different PHP version (ex: Installing an extension for PHP 7.0 would remove the 5.6 version if the package was still registered as installed).

Monday, December 12, 2022
1

Strict comparison (===) will always be slightly faster, but the difference is usually negligible.

It definitely makes sense to prefer === if you know for certain that you don't need type coercion in the comparison. It will always be at least as fast as ==.

Wednesday, November 23, 2022
 
2

You want records from Foo where Bar = @param, or if @param is null, where Bar is null. Some of the proposed solutions will give you null records with nonnull @param, which does not sound like your requirement.

Select * from Foo where (@param is null and Bar is null) or (Bar = @param)

This doesn't say whether this is Oracle or SQL Server or another RDBMS, because they each implement slightly different helper functions. SQL's ISNULL(first, second) like NVL(first, second). I like SQL Server's COALESCE() for the general applicability.

The IS comparison is only for null comparisons.

If you are using SQL Server and if you really need a different 3VL logic truth table to solve your problem (that is, if you have a specific need for "NULL=NULL" to be "true" at some point in time, and also recognize that this is deprecated and barring your reasons, not a good idea in general), within your code block you can use the directive

SET ANSI_NULLS OFF

Here's the BOL on it: http://msdn.microsoft.com/en-us/library/ms188048.aspx

Thursday, November 24, 2022
 
4

Never used any of those, but they look interesting..

Take a look at Gearman as well.. more overhead in systems like these but you get other cool stuff :) Guess it depends on your needs ..

Friday, November 11, 2022
 
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :
 
Share