• Skip to main content
  • Skip to primary sidebar
  • Skip to footer

Matt Doyle | Elated Communications

Web and WordPress Development

  • About Me
  • Blog
    • Design & Multimedia
      • Photoshop
      • Paint Shop Pro
      • Video & Audio
    • Online Marketing
      • E-Commerce
      • Social Media
    • Running a Website
      • WordPress
      • Apache
      • UNIX and Linux
      • Using FTP
    • Web Development
      • HTML
      • CSS
      • JavaScript
      • PHP
      • Perl and CGI Scripting
  • Portfolio
  • Contact Me
  • Hire Me
Home / Blog / Web Development / PHP / Making Decisions with the PHP if Statement

Making Decisions with the PHP if Statement

14 July 2009 / Leave a Comment

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!

Filed Under: PHP Tagged With: branching, decisions, else statement, if statement, php else, php if

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

To include a block of code in your comment, surround it with <pre> ... </pre> tags. You can include smaller code snippets inside some normal text by surrounding them with <code> ... </code> tags.

Allowed tags in comments: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre> .

Primary Sidebar

Hire Matt!

Matt Doyle headshot

Need a little help with your website? I have over 20 years of web development experience under my belt. Let’s chat!

Matt Doyle - Codeable Expert Certificate

Hire Me Today

Call Me: +61 2 8006 0622

Stay in Touch!

Subscribe to get a quick email whenever I add new articles, free goodies, or special offers. I won’t spam you.

Subscribe

Recent Posts

  • Make a Rotatable 3D Product Boxshot with Three.js
  • Speed Up Your WordPress Website: 11 Simple Steps to a Faster Site
  • Reboot!
  • Wordfence Tutorial: How to Keep Your WordPress Site Safe from Hackers
  • How to Make Awesome-Looking Images for Your Website

Footer

Contact Matt

  • Email Me
  • Call Me: +61 2 8006 0622

Follow Matt

  • E-mail
  • Facebook
  • GitHub
  • LinkedIn
  • Twitter

Copyright © 1996-2023 Elated Communications. All rights reserved.
Affiliate Disclaimer | Privacy Policy | Terms of Use | Service T&C | Credits