PHP comparison syntax: equality

Posted December 27, 2003 by Quinn McHenry in PHP programming

The equality comparison is a fundamental operation in programming languages. In PHP, to evaluate if one expression is equal to another, use the == comparison as described in this Tech-Recipe.


if ($var1 == $var2)
echo "Yep, they're equal.";

Warning: the code inside the expression can be any valid PHP code, so if a function returns a value, you can assign that value to a variable and use the return value in a comparison. A common mistake in writing equality conditionals is using a single = instead of a double ==. The conditional expression ($var1 = $var2) assigns the value of $var2 to $var1 and always evaluates as true.

 

About Quinn McHenry

Quinn was one of the original co-founders of Tech-Recipes. He is currently crafting iOS applications as a senior developer at Small Planet Digital in Brooklyn, New York.
View more articles by Quinn McHenry

The Conversation

Follow the reactions below and share your own thoughts.