php 101

Conditionals and Operators

A big part of programming is in performing tests. "If the following statement is true, then do something." "If user is logged in, show the logout link. Otherwise show the login link." "If the user has checked the I Agree checkbox, then let them download the file. Otherwise prompt them to check the box." And so on.

A statement that performs a test and does something based on the result is called a "conditional" (because it responds to conditions). The tools a conditional uses to perform tests are called "conditional operators" and are usually of the form "If something is greater than something else" or "If these two strings match" or "If something is true." So you need to learn conditionals and conditional operators at the same time.

Here's the general form of a "test" in PHP, using the "if" statement:

Note that you do not use a semicolon after the curly braces - this is a rare exception to the "end every line with a semi-colon in PHP" rule. Often, you'll want to do one thing if a condition is met, and another thing if it is not:

In fact, you may want to respond to several possible conditions. You can do this with the elseif() statement:

You can have as many elseif() clauses as you like.

Note: As PHP is evaluating the conditions in the block, it will exit the block as soon as the first condition is met. This can mean that tests farther down in the block are never seen by the interpreter if conditions above it are met. In other words, the order of tests in the block can be very important! Think carefully when setting up conditions.

So what goes in those (condition) sections? Usually you'll be testing to see whether two things are equal, or if one is greater than another. Let's say you need to determine whether a student has passed a test (assume it takes a score of 70 to pass). Your script might pull test scores out of a database, compare them to a constant (the threshold for passing), and email the student to let them know whether they passed. We'll re-use the $scores array from earlier.

In this case, we perform a test (line 5) to see whether the array element named 'Scot' has a value that is greater than or equal to the currently established threshold for passing the course. Try changing 'Scot' to 'Josh' to see the script output the opposite result.

Here's a complete table of the operators you can use. The conditional operators are fairly clear, and you should remember most of them from grade-school math classes. Note that some of the operators are capable of comparing both the value and the datatype of the variable being tested.

Example Name Result
$a == $b Equal TRUE if $a is equal to $b.
$a === $b Identical TRUE if $a is equal to $b, and they are of the same type.
$a != $b Not equal TRUE if $a is not equal to $b.
$a <> $b Not equal TRUE if $a is not equal to $b.
$a !== $b Not identical TRUE if $a is not equal to $b, or they are not of the same type.
$a < $b Less than TRUE if $a is strictly less than $b.
$a > $b Greater than TRUE if $a is strictly greater than $b.
$a <= $b Less than or equal to TRUE if $a is less than or equal to $b.
$a >= $b Greater than or equal to TRUE if $a is greater than or equal to $b.