Difference between == and === in PHP


The == operator just checks to see if the left and right values are equal.

‘==’ (Equal):

if("34" == 34){
     echo "YES";
}else{
    echo "NO";
}

The === operator (note the extra “=”) actually checks to see if the left and right values are equal, and also checks to see if they are of the same variable type (like whether they are both booleans, ints, etc.).

‘===’ (Identical):

if("34" == (string)34){
     echo "YES";
}else{
    echo "NO";
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s