News & UpdatesProgrammingWeb programming StoreMy Projects
Links
Affiliates

PHP Tutorial – 07 – Conditionals

Conditional statements are used to execute different code blocks based on different conditions.

If statement

The if statement will only execute if the condition inside the parentheses is evaluated to true. The condition can include any of the comparison and logical operators. In PHP, the condition does not have to be a Boolean expression.

if ($x == 1) { 
  echo "x is 1"; 
}

To test for other conditions, the if statement can be extended with any number of elseif clauses. Each additional condition will only be tested if all previous conditions are false.

elseif ($x == 2) { 
  echo "x is 2"; 
}

The if statement can have one else clause at the end, which will execute if all previous conditions are false.

else { 
  echo "x is something else"; 
}

As for the curly brackets, they can be left out if only a single statement needs to be executed conditionally.

if ($x == 1) 
  echo "x is 1"; 
elseif ($x == 2) 
  echo "x is 2";
else 
  echo "x is something else";

Switch statement

The switch statement checks for equality between an integer, float or string and a series of case labels. It then passes execution to the matching case. The statement can contain any number of case clauses and may end with a default label for handling all other cases.

switch ($x)
{
  case 1: echo "x is 1"; break;
  case 2: echo "x is 2";  break;
  default:echo "x is something else";
}

Note that the statements after each case label are not surrounded by curly brackets. Instead, the statements end with the break keyword to break out of the switch. Without the break the execution will fall through to the next case. This can be useful if several cases need to be evaluated in the same way.

Alternative syntax

PHP has an alternative syntax for the conditional statements. In this syntax, the if statement’s opening bracket is replaced with a colon, the closing bracket is removed, and the last closing bracket is replaced with the endif keyword.

if ($x == 1):     echo "x is 1";  
elseif ($x == 2): echo "x is 2";
else:             echo "x is something else";
endif;

Similarly, the switch statement also has an alternative syntax, which instead uses the endswitch keyword to terminate the statement.

switch ($x):
  case 1:  echo "x is 1"; break;
  case 2:  echo "x is 2";  break;
  default: echo "x is something else";
endswitch;

The alternative syntax is often preferable for longer conditional statements since it then becomes easier to see where those statements end.

Mixed modes

It is possible to switch back to HTML mode in the middle of a code block. This provides another way of writing conditional statements that output text to the web page.

<?php if ($x == 1) { ?>
  This will show if $x is 1.
<?php } else { ?>
  Otherwise this will show.
<?php } ?>

The alternative syntax may also be used in this way to make the code clearer.

<?php if ($x == 1): ?>
  This will show if $x is 1.
<?php else: ?>
  Otherwise this will show.
<?php endif; ?>

When outputting HTML and text, particularly larger blocks, this coding style is generally preferred as it makes it easier to distinguish between PHP code and the HTML content that appears on the web page.

Ternary operator

In addition to the if and switch statements there is the ternary operator (?:). This operator can replace a single if/else clause. The operator takes three expressions. If the first one is evaluated to true then the second expression is returned, and if it is false, the third one is returned.

// Ternary operator expression
$y = ($x == 1) ? 1 : 2;

In PHP, this operator cannot just be used as an expression, but also as a statement.

// Ternary operator statement
($x == 1) ? $y = 1 : $y = 2;

The programming term expression refers to code that evaluates to a value, whereas a statement is a code segment that ends with a semicolon or a closing curly bracket.

Recommended additional reading:
Sams - Teach Yourself PHP in 24 Hours