2.1. Setting Up Conditions

Conditions are expressions that PHP tests or evaluates to see whether they are true or false. Conditions are used in complex statements to determine whether a block of simple statements should be executed. To set up conditions, you compare values. Here are some questions you can ask to compare values for conditions:

  • Are two values equal? Is Sally's last name the same as Bobby's last name? Or, is Nick 15 years old? (Does Nick's age equal 15?)

  • Is one value larger or smaller than another? Is Nick younger than Bobby? Or, did Sally's house cost more than a million dollars?

  • Does a string match a pattern? Does Bobby's name begin with an S? Does the ZIP code have five numeric characters?

You can also set up conditions in which you ask two or more questions. For example, you may ask: Is Nick older than Bobby and is Nick younger than Sally? Or you may ask: Is today Sunday and is today sunny? Or you may ask: Is today Sunday or is today Monday?

2.1.1. Comparing values

You can compare numbers or strings to see whether they are equal, whether one is larger than the other, or whether they are not equal. You compare values with comparison operators. PHP evaluates the comparison and returns true or false. For example, the following is a simple comparison:

$result = $a == $b;

The comparison operator == checks whether two values are equal. If $a and $b are equal, $result is assigned the Boolean value true. If $a and $b are not equal, $result is assigned false. Thus, $a ...

Get PHP & MySQL® Web Development All-in-One Desk Reference for Dummies® now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.