PHP comparison syntax: equality

Contributor Icon Contributed by qmchenry Date Icon December 27, 2003  
Tag Icon Tagged: 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.

Previous recipe | Next recipe |
 

close Reblog this comment
blog comments powered by Disqus