Double and Triple equal operators in PHP

Even if you're a seasoned programmer, you may not have seen triple equals operator in most programming languages. In PHP, the triple equals (===) operator was introduced in version 4 and it checks for equality similar to double equals (==) operator but also checks type of variable. Here are the differences between single, double and triple equals operators.

A single equals operator (=) is an assignment operator - take what's on the right as an expression and save it in the variable named on the left.

$a = 5;  // A is assigned an integer value 5.

A double equals operator (==) is a comparison operator and it tests the value (variable, expression or constant) of left to the right for equality. If the values are the same, it returns true.

$a = 5;
$b = "5";
if ($a == $b) echo "same";  // Returns TRUE

A triple equals operator (===) is a comparison operator and it tests the value (variable, expression or constant) of left to the right for identicalness. The value of left and right have to be equal AND the type has to be equal as well. (i.e. both are strings or both are integers).

$a = 5;
$b = "5";
if ($a === $b) echo "same";  // Returns FALSE

For more information about equals operators, please visit PHP Manual.

Tags: 

Comments

Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.