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.

Some examples using the if statement

Here’s a simple example script showing the PHP if statement in action:
<?php
$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 $numWidgetsvariable 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?
<?php

$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:
<?php
$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".

Making Decisions with the PHP elseif Statement

The PHP if and else statements let a script decide whether to run a chunk of code based on a condition. The if statement runs a chunk of code if the condition is true or skips it if the condition is false. ifcombined with else runs one chunk if the condition is true, or the other chunk if the condition is false.
PHP lets you take things further, and chain several decision-making blocks together, with each block having its own condition to test. To do this, you use the elseif statement.

Syntax of the PHP elseif statement

The simplest form of an if ... elseif block has the following structure:
if ( condition1 ) {
  // This code is run if condition1 is true
} elseif ( condition2 ) {
  // This code is run if condition1 is false and condition2 is true
}
 
// This code is run anyway
 
Here’s how it works:
  • Firstly, condition1 is tested. If it’s true then the first block of code — between the if and elseifstatements — is run. The PHP engine then skips to the first line of code after the whole if ... elseif block (the last line of code in the above example).
  • If condition1 is false then condition2 is tested. If this condition is true then the second block of code — between the elseif statement and the final closing brace — is run. Once again, the PHP engine then skips to the first line of code after the if ... elseif block.
  • If condition2 is also false then neither blocks of code inside the if ... elseif block are run. Execution continues with the first line of code after the if ... elseif block.
You can have as many elseif blocks as you like. Each condition in turn is tested; if it’s true then the code in the block is run, otherwise the PHP engine moves onto the next elseif block:
 
if ( condition1 ) {
  // This code is run if condition1 is true
} elseif ( condition2 ) {
  // This code is run if condition1 is false and condition2 is true
} elseif ( condition3 ) {
  // This code is run if condition1 and condition2 are false and condition3 is true
}
 
// This code is run anyway
You can also add an else block after your elseif block(s). The else block is run if all the previous if andelseif conditions are false:
if ( condition1 ) {
  // This code is run if condition1 is true
} elseif ( condition2 ) {
  // This code is run if condition1 is false and condition2 is true
} else {
  // This code is run if neither condition1 or condition2 are true
}
 
// This code is run anyway
If you prefer, you can write elseif as two words: else if.

A PHP elseif example

Here’s an example that uses ifelseif and else:
<?php

$numWidgets = 2;

if ( $numWidgets > 2 ) {
echo "We have more than 2 widgets in stock<br />";
} elseif ( $numWidgets < 2 ) {
echo "We have less than 2 widgets in stock<br />";
} else {
echo "We have exactly 2 widgets in stock<br />";
}

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

?>

This code displays:
We have exactly 2 widgets in stock
Number of widgets: 2

0 comments:

Post a Comment

 
Top
Blogger Template