Conditionals and Operators

Conditionals, like variables, are integral to programming, and most people are familiar with them in some form or another. Dynamic Web pages, as you might imagine, frequently require the use of conditionals to alter a script’s behavior according to set criteria.

PHP’s three primary terms for creating conditionals are if, else, and elseif (which can also be written as two words, else if).

Every conditional includes an if clause:

if (condition) {

   // Do something!

}

This in turn can become

if (condition) {

   // Do something!

} else {

   // Do something else!

}

and

if (condition1) {

   // Do something!

} elseif (condition2) {

   // Do something else!

} else {

   // Do something different!

}

If a condition is true, the code in the ...

Get PHP and MySQL for Dynamic Web Sites: Visual Quickpro Guide, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.