Conditionals
Conditionals alter program flow. They enable you to ask questions about certain things and respond to the answers you get in different ways. Conditionals are central to dynamic web pages—the goal of using PHP in the first place—because they make it easy to create different output each time a page is viewed.
There are three types of nonlooping conditionals: the if statement, the switch statement, and the ? operator. By nonlooping, I mean that the
actions initiated by the statement take place and program flow then moves
on, whereas looping conditionals (which we’ll come to shortly) execute
code over and over until a condition has been met.
The if Statement
One way of thinking about program flow is to imagine it as a single-lane highway that you are driving along. It’s pretty much a straight line, but now and then you encounter various signs telling you where to go.
In the case of an if statement,
you could imagine coming across a detour sign that you have to follow if
a certain condition is TRUE. If so,
you drive off and follow the detour until you rejoin your original
route; you then continue on your way in your original direction. Or, if
the condition isn’t TRUE, you ignore
the detour and carry on driving (see Figure 4-1).

The contents of the if condition can be any valid PHP expression, including equality, comparison, ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access