Decision Making in PHP

Decision Making in PHP

Any PHP script is built out of a series of statements. A statement can be an assignment, a function call, a loop, a conditional statement of even a statement that does nothing (an empty statement). Statements usually end with a semicolon. In addition, statements can be grouped into a statement-group by encapsulating a group of statements with curly braces. A statement-group is a statement by itself as well.

  1. If Statement
  2. Else Statement
  3. Elseif Statement
  4. Switch Statement

If Statement

An if statement is a way of controlling the execution of a statement that follows it (that is, a single statement or a block of code inside braces). The if statement evaluates an expression between parentheses. If this expression results in a true value, the statement is executed. Otherwise, the statement is skipped entirely. This enables scripts to make decisions based on any number of factors:

Syntax:-

if ( expression ) {
// code to execute if the expression evaluates to true
}

PHP Example:- The following code would display a is bigger than b if $a is bigger than $b:

<?php
$a=10;
$b=5;
if ($a > $b)
echo "a is bigger than b";
?>

Else Statement

When working with the if statement, you will often want to define an alternative block of code that should be executed if the expression you are testing evaluates to false. You can do this by adding else to the if statement followed by a further block of code:

Syntax:-

if ( expression ) {
// code to execute if the expression evaluates to true
} else {
// code to execute in all other cases
}

The following example would display a is bigger than b if $a is bigger than $b, and a is NOT bigger than b otherwise:

<?php
$a=10;
$b=20;
if ($a > $b) {
echo "a is bigger than b";
}
else {
echo "a is NOT bigger than b";
}
?>

The else statement is only executed if the if expression evaluated to FALSE.

Elseif Statement

As its name suggests, is a combination of if and else. Like else, it extends an if statement to execute a different statement in case the original if expression evaluates to FALSE. However, unlike else, it will execute that alternative expression only if the elseif conditional expression evaluates to TRUE.

Syntax:-

if ( expression ) {
// code to execute if the expression evaluates to true
}
elseif (another expression) {
// code to execute if the previous expression failed
// and this one evaluates to true
}
else {
// code to execute in all other cases
}

PHP Example:- The following code would display a is bigger than b, a equal to b or a is smaller than b:

<?php
$a=10;
$b=20;
if ($a > $b) {
echo "a is bigger than b";
} elseif ($a == $b) {
echo "a is equal to b";
} else {
echo "a is smaller than b";
}
?>

Switch Statement

The switch statement is similar to a series of IF statements on the same expression. In many occasions, you may want to compare the same variable (or expression) with many different values, and execute a different piece of code depending on which value it equals to. This is exactly what the switch statement is for.

Syntax:-

switch ( expression ) {
case result1:
// execute this if expression results in result1
break;
case result2:
// execute this if expression results in result2
break;
default:
// execute this if no break statement
// has been encountered hitherto
}
<?php
$i=10;
switch ($i) {
case 0:
echo "i equals 0";
break;
case 1:
echo "i equals 1";
break;
case 2:
echo "i equals 2";
break;
default:
echo "i is greater than 3";
}
?>