Making Decisions with the PHP if Statement

Learn how to use the PHP if statement to make decisions in your scripts. Also covers the PHP else statement.

The PHP if statement lets a script test to see if a certain condition is true, and run a chunk of code if it is. This allows your code to make decisions about what to do next, thereby adding a lot of power to your scripts.

PHP if syntax

A PHP if construct takes the following general format:


if ( condition ) {
  // This code is run if condition is true
}

// This code is run anyway

Here's how it works:

  1. The PHP engine tests the condition inside the parentheses.
  2. If the condition is true then the PHP engine runs the code inside the braces ({}), then it starts running any code after the closing brace.
  3. If the condition is false then the PHP engine skips the code inside the braces, and starts running any code after the closing brace.

You can use any expression as the condition in an if statement, as long as it evaluates to either true or false. You can use one or more PHP operators to build your condition expression.

Some examples using the if statement

Here's a simple example script showing the PHP if statement in action:


$numWidgets = 3;

if ( $numWidgets > 1 ) {
  echo "We have more than 1 widget in stock!<br />";
}

echo "Number of widgets: $numWidgets<br />";

When run, this code displays the following in a Web browser:


We have more than 1 widget in stock!
Number of widgets: 3

The script sets a $numWidgets variable to 3, then tests the variable to see if its value is greater than 1. Since it is, the code inside the braces is run and the message "We have more than 1 widget in stock!" is displayed. Finally the code after the if block is run, displaying "Number of widgets: 3".

What happens if $numWidgets isn't greater than 1?


$numWidgets = 0;

if ( $numWidgets > 1 ) {
  echo "We have more than 1 widget in stock!<br />";
}

echo "Number of widgets: $numWidgets<br />";

This code displays:


Number of widgets: 0

Since $numWidgets isn't greater than 1, the code inside the if block is skipped and the first message isn't displayed.

Choosing between chunks of code with if ... else

You can enhance the if statement with the else statement. This lets you run one chunk of code if a condition is true, or a different chunk of code if the condition is false:


if ( condition ) {
  // This code is run if condition is true
} else {
  // This code is run if condition is false
}

// This code is run anyway

Here's an example that uses if ... else:


$numWidgets = 0;

if ( $numWidgets >= 1 ) {
  echo "We have at least 1 widget in stock<br />";
} else {
  echo "We have less than 1 widget in stock<br />";
}

echo "Number of widgets: $numWidgets<br />";

This code displays:


We have less than 1 widget in stock
Number of widgets: 0

$numWidgets is less than 1, so the condition $numWidgets >= 1 is false. Therefore the PHP engine runs the code inside the else block, displaying the message "We have less than 1 widget in stock".

You now know how to use the PHP if statement to make decisions about which piece of code to run. You've also seen how to use the else statement to choose between two different blocks of code. Happy coding!

Learn PHP With Ease!

Written by Matt Doyle — ELATED's resident Web programming expert — Beginning PHP 5.3 is a complete introduction to PHP, covering everything in these tutorials and lots more besides. Find out how to:

  • Set up PHP on your computer
  • Use strings, arrays, functions and objects
  • Create interactive Web forms
  • Handle cookies and sessions
  • Work with files on the server
  • Build database-driven sites with MySQL
  • Send emails from your scripts
  • Create images on the fly with PHP
  • Work with regular expressions
  • Write robust, secure PHP applications

...and lots more!

“What a pleasure it's been spending hours and hours studying PHP with this magical book.” — Lulio, Florida
“The book is not only great for learning, but I find myself using it constantly as a reference as well!” — David A. Stoltz

Buy Beginning PHP 5.3 now from Amazon.comBeginning PHP 5.3 or Amazon.co.ukBeginning PHP 5.3.

Follow Elated

Related articles

Responses to this article

There are no responses yet.

Post a response

Want to add a comment, or ask a question about this article? Post a response.

To post responses you need to be a member. Not a member yet? Signing up is free, easy and only takes a minute. Sign up now.

Top of Page