HomeWeb application programmingPHP programmingPHP comparison syntax: equality

PHP comparison syntax: equality

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.

Quinn McHenry
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.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

LATEST REVIEWS

Recent Comments

error: Content is protected !!