Growing up, the statement "you can have your dessert if you finish your vegetables first" was very familiar to me. It was an early introduction to the wonderful world of conditions, which are very common in our everyday lives. Whether we think about it or not, most decisions we make are based on at least one condition being met--or not met. Conditions are very important in programming.

The "if statement" is the most common conditional statement used in php. It will perform an action (such as serving dessert) if the specified condition is met, or true (such as finishing the vegetables). If the condition is not met, or false, the action will not be performed.

The if statement syntax places the condition in parenthesis, followed by the result in curly brackets, in the following manner: if (condition) { action to take }

  $number = 5;
  if ($number>3) { echo "Condition is met!"; }

Remember in our last chapter how we studied php comparison operators? The "is greater than" comparison operator ">" is used in this example to compare two values. Our example, when translated to English, says: if the value of the numbers variable is greater than 3, echo that the condition is met. Since this particular condition is true, the action will be performed.

Conditions are not at all limited to comparing numerical values.

  $tell_joke = "yes";
  if ($tell_joke=="yes") {
    echo "How do you spot a modern spider?<br>";
    echo "She doesn't have a web she has a website!<br>";

The practical applications of if statements will become more clear as we progress, because we'll be seeing a lot more of them, beginning on the next page... or else!