Vivek Soni

Vivek Soni: Strong theoretical - yet practical - and technical background with a Master of Computer Science (with Business Option). Read More

Difference between ‘==’ (equal) and ‘===’ (identical) comparison operators in PHP (with examples)

August 31, 2010 | Author: vivek kumar | Filed under: PHP

Two of the many comparison operators used by PHP are ‘==’ (i.e. equal) and ‘===’ (i.e. identical). The difference between the two is that ‘==’ should be used to check if the values of the two operands are equal or not. On the other hand, ‘===’ checks the values as well as the type of [...]

Two of the many comparison operators used by PHP are ‘==’ (i.e. equal) and ‘===’ (i.e. identical). The difference between the two is that ‘==’ should be used to check 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.

Let me explain more using some examples:
‘==’ (Equal):

if(“22″ == 22) echo “YES”;
else echo “NO”;

The code above will print “YES”. The reason is that the values of the operands are equal. Whereas when we run the example code below:
‘===’ (Identical):

if(“22″ === 22) echo “YES”;
else echo “NO”;

The result we get is “NO”. The reason is that although values of both operands are same their types are different, “22″ (with quotes) is a string while 22 (w/o quotes) is an integer. But if we change the code above to the following:

if(“22″ === (string)22) echo “YES”;
else echo “NO”;

Then, the result will be “YES”. Notice that we changed the type of right operand to a string which is the same as the left operand (i.e. string). Now, the types and values of both left and right operands are the same hence both operands are identical.

Aug 2010 20

Installing ImageMagick on Windows — Setup Imagick on PHP

I was trying to install ImageMagick on Windows. I got the correct binary file, but when I tried to invoke it with the PHP extension (php_imagick.dll) it was...

Aug 2010 10

SMTP server response: 550 5.7.1 Unable to relay – *A Solution*

I just spent the last 2 hours chasing my tail trying to figure out a install problem I was having with Drupal: Symptom When logging into Drupal for...

Aug 2010 9

Secure your Ajax requests with jQuery

Ajax requests suffer from the same Cross Site Request Forgery attack vectors as normal pages. Many developers assume that a given ajax request will only take place on...

Aug 2010 5

SQL Injection Attack – Examples and Preventions in PHP

What is SQL injection? It is a basically a trick to inject SQL command or query as a input mainly in the form of the POST or GET...

Aug 2010 4

PHP: How to Make Life Easier with References

Don’t you just hate it when you have to write very long codes such as below? $name = mysql_real_escape_string(strip_tags($name)); $email = mysql_real_escape_string(strip_tags($email)); $address = mysql_real_escape_string(strip_tags($address)); Even if you...

Aug 2010 4

jQuery Autosave

Features * Send entire form when one element changes * Send just the changed element * Form buttons and input:submit is not sent unless clicked * Override action/method...

Aug 2010 3

HowTo: Turn off MySQL Strict Mode

By default, MySQL is enabled with strict_mode ON. Mambo currently does not fully support MySQL5.x in strict_mode (this is on the roadmap and will be dealt with when...

Jul 2010 21

Facebook’s scaling challenge

Before we get into the details, here are a few factoids to give you an idea of the scaling challenge that Facebook has to deal with: * Facebook...

Jul 2010 15

PHP comparison GOTCHAS

I thought most of the programmer understand the comparison operators in PHP and what NULL was in the language. I was sadly mistaken and it caused me a...

Recent Comments

  • free tutorials: Hey thanks a lot for sharing useful interview questions….. which will be very helpful while...
  • priya: Informatiomns are useful.
  • kelley: dude – where have you been. you’re not on facebook anymore? all the posts between us are gone. :(
  • deepak: a brilliancy for this document i have remarked
  • Kelley: A good time to write documentation is when someone in the department gives two-weeks notice: use that time to...