=== operator in PHP

This is the code from blog demo

public function loadModel()







Somebody told me before "===" not only compare the value is equal, but also compare if the data type is the same. Is this claim true? Can you tell me where to find reference of the === operator?

If it’s true, then what the difference does it make in using == or === in the following code?

if($this->_model===null) {}

Thank you!

yes it’s true.

=== compare data type and value,but

== compare vale only.


$a = "";

$a == null --> true

but $a === null --> false


if($this->_model===null) {}

Since no data type will have the type "null", the above code will always be false. is this true?

$this->model defaults to null, so it will be true if $this->model has not been initialised yet.

So in this case, why not just use ==, instead of === ?

=== is faster than == since there is no type-conversion. The speed difference is most likely negligible, though we use it everywhere in the core.

Personally I think it should be common practice to always use === if it fits. I like strictness and I see no reason to spend additonal CPU time for nothing.

Regarding the == and ===

Check this code



   // if $level IS zero... do some very critical operation

   echo "IS zero";




   // if $level is not zero just output an error

   echo "NOT zero";


Now… the code is very simple and there is nothing wrong with it… right… or is there?

Try to assign the variable $level any text like $level="anytextyoulike" and check the result…

I leave to you to find the reason why this is happening…

by doing this you will understand best the difference between == and === ;)

By running your code, I got "Notice: Undefined variable: level in C:\xampp\htdocs\test.php on line 4

IS zero". It’s the $level isn’t defined. I think I failed to understand your point. What shall I do?


I understand your argument. Thanks!

Before the if statement… you need to assign a value to the variable… like I suggested put

$level="any text you like";

before the if statement