Phpdynamic

PHP and MySQL Programming Guides, Tutorials, & Articles

PHP Conditional Statements

PHP Conditional Statements

MAR 31

Implementing Logic in PHP

The examples we’ve looked at so far have been pretty straight-forward and not very intelligent. The code was executed without any built-in logic, that’s all fun and games, but to create scripts that can respond to different situations and alter the normal flow of control in response to those situations, we need to utilize conditional statements.

What is flow of control? Normal flow of control is the sequential order of execution within a program; one statement after another as they occur. With conditional statements, we can control this flow and implement functionality that allows PHP to choose which statements to execute under different conditions.

The If Statement

The if statement is an indispensable weapon in the PHP language arsenal and will be widely used for the majority of time in your scripts. Lets begin by looking at the syntax template, notice if you’ve ever coded with other programming languages such as C, C++, or Javascript, the syntax is very similar if not the same.

if (condition)
{
  // if condition is TRUE then execute the following
  statement1
  statement2
  ...
}

The if statement tests a logical condition surrounded by parentheses ( ) and if that condition returns true, PHP will execute the following code enclosed by the curly { } braces, otherwise if the condition returns false PHP will skip that code. Lets look at an example and break it down step by step.

$num = 20;

if ($num > 10 && $num < 20)
{
  echo '<p>$num is between 10 and 20</p>';
}

This is the output if you execute the code above:

$num is between 10 and 20

This is the English language equivalent for the example above: if the variable $num is greater than 10 and less than 20, output the following message "$num is between 10 and 20" to the browser.

In other words, we are asking PHP to perform an action based on whether a condition exists. Since we assigned the value 20 to the $num variable, the condition in the if statement is true and therefore the code within the curly { } braces is executed. If we assign $num a value higher than 20 and run the script again, PHP will ignore the code within the curly { } braces and continue onto the next statements, since the condition now evaluates to false.

Adding an Else Clause

Lets say you want PHP to take a different course of action if the condition in the if statement is false. We can implement this by using the Else clause. Lets expand on the previous example:

$num = 38;

if ($num > 10 && $num < 20)
{
  echo '<p>$num is between 10 and 20</p>';
}
else
{
  echo '<p>$num is out of range</p>';
  echo '$num is '.$num;
}

The output:

$num is out of range
$num is 38

Curly { } braces are used to group a sequence of PHP statements together to form a single code block. PHP treats code blocks as single entities and all statements within a code block are executed together as a group instead of independently. Notice in this example we have two code blocks, one belonging to the if statement and the other to the else statement. If the condition evaluates to true, PHP will execute the first code block else if it returns false PHP will skip the first code block and execute the second block.

Else-if Statements

Else-if statements allow us to build more complex logic into our scripts were we can have more than two possible outcomes depending on the condition being tested. In this example we test a sequence of conditions and display a message that more closely relates to the value stored in $num.

$num = 38;

if ($num > 10 && $num < 20)
{
  echo '<p>$num is between 10 and 20</p>';
}
elseif ($num > 20 && $num < 30)
{
  echo '<p>$num is between 20 and 30</p>';
}
elseif ($num > 30 && $num < 40)
{
  echo '<p>$num is between 30 and 40</p>;
}
else
{
  echo '<p>$num is out of range</p>';
}

And this is the output:

$num is between 30 and 40

In this example, we have provided PHP with more than two possible outcomes to choose from depending on a condition. PHP will start at the first if statement and work its way down checking each condition until it finds one that is true. When it finds a condition that exists, the code block following the true condition will be executed.

You might be asking, isn’t it possible to test this with a bunch of if statements? Well, it is possible, but you will not get the expected results, lets demonstrate this with an example, but this time we’ll assign $num a value of 12.

$num = 12;

if ($num > 10 && $num < 20)
{
  echo '<p>$num is between 10 and 20</p>';
}
if ($num > 20 && $num < 30)
{
  echo '<p>$num is between 20 and 30</p>';
}
if ($num > 30 && $num < 40)
{
  echo '<p>$num is between 30 and 40</p>';
}
else
{
  echo '<p>$num is out of range</p>';
}

Now if you run the script, you’ll get the following result:

$num is between 10 and 20
$num is out of range

Not exactly the result we were look for. The if statements are executed independently of each other, even though the condition in the first if statement is satisfied. Since the if statements are executed separately, more than one action can occur at the same time. On the other hand when using the else-if method, PHP will only choose one outcome that corresponds to the first condition that returns true and skips the remaining conditions. As you learn more of the language, you’ll be better suited to select the proper conditional statements that fit your needs.

Switch Statement

Switch statements are similar to if-else statements with a few differences. In addition to testing an expression for a true or false value; switch statements can be used to test expressions that can result in a number of different values. Switch statements are well suited for comparing a variable with different values and performing different actions depending on the value.

$num = 3;

switch ($num) {
  case "1" :
    echo '$num is 1';
    break;
  case "2" :
	echo '$num is 2';
	break;
  case "3" :
	echo '$num is 3';
	break;
  default :
	echo '$num is out of range';
}
$num is 3

In our example, the third case statement matches the value we assigned to $num (3). PHP executes the code following that case statement until in reaches the break statement. The default statement only executes if none of the case statements are true.

  • Share/Bookmark

You can leave a response, or trackback from your own site.

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>