PHP Conditional Statements

Like other programming languages, PHP also offers conditional statements through which you can create test cases, also called expressions which return either true or false. This process facilitates to perform certain actions based on the result of specific logical condition.

<?php

If (expression)

        Statement;

?>

Conditional statements add a lot of life to PHP. A conditional statement says something like the following: If you like pizza you will eat pizza , otherwise (elseyou won’t eat pizza. This ability to choose between options will make your PHP seem like it can “think” like a human can.

In PHP we have the following conditional statements:

  • if statement – executes some code if one condition is true
  • if…else statement – executes some code if a condition is true and another code if that condition is false
  • if…elseif….else statement – executes different codes for more than two conditions
  • switch statement – selects one of many blocks of code to be executed.

PHP – The if Statement

The if statement executes some code if one condition is true.

Syntax

if (condition) {
code to be executed if condition is true
;
}

Be careful that semicolons are necessary at each statement end and it is not important that you go to the next line or write new code exactly after that.

Now let’s take a look at an example.

<?php
  $i_like_pizza = true;
  if ($i_like_pizza == true) {
    echo "I eat pizza!";
  }
  else {
    echo "I don't eat pizza!";
  }
?>

The output is:

I eat pizza!

What we’ve done is set the variable $i_like_pizza to true. We then use this in our if/else statement. We could use anything in the if statement but this is easy and we’ll look at some other options next.

Whatever we do “if” the condition between () is true is contained between the curly brackets {}. Whatever we do if the condition is false is contained after the “else” statement and between the next set of curly brackets.

PHP  if…else…elseif Statements

If you want to have more complex conditions, doing something if a condition is true and do something else if not, you must use ‘else’ statement.

elseif, as its name suggests, is a combination of if and else. It is the command that let us to break ‘else’ part to more cases.  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.

How it works:

When the if main condition gets false, php goes to else part; by elseif command in this part another condition must be checked by php, if the answer of this new condition gets true, the operation specified here would be ran, otherwise, if we had another ‘elseif’ the procedure will be repeated for it, and if the answer gets false, php goes to the final else part.

This means that we have the case that doesn’t match with all conditions above, and the operation mentioned here will work. These parts are optional and can be removed from main if block.

Look at the example below to get it more obvious for you:

<?php
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";
}
?>

The above code would display a is bigger than ba equal to b or a is smaller than b: As you can see, the if else statement executes some code if a condition is true and an additional code if a condition is false.

PHP Switch Case

PHP if-else statement that needs more than one condition can replaced by switch case in PHP.

The amount of choices builds, the complexity of such code also increases. Thus, to make it less troublesome, PHP supports multi-way choice statement regarded as switch.

Syntax for PHP switch Statement:

Switch(var)
{
   case label 1: code to be executed if var=label1;
                 break;

   case label 2: code to be executed if var=label2;
                 break;

   default: code to be executed if var is different from both label1 and label2;
}

Example on PHP switch Statement:

<?php
switch($number)
{
   case 1: echo "The Number is 1.";
           break;
   
   case 2: echo "The Number is 2.";
           break; 
   
   case 3: echo "The Number is 3.";
           break;

   default: echo "The Number is Greater than 3.";
}
?>

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.

he switch statement is used to perform different actions based on different conditions.

Use the switch statement to select one of many blocks of code to be executed.

Add a Comment

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

ABOUT CODINGACE

My name is Nohman Habib and I am a web developer with over 10 years of experience, programming in Joomla, Wordpress, WHMCS, vTiger and Hybrid Apps. My plan to start codingace.com is to share my experience and expertise with others. Here my basic area of focus is to post tutorials primarily on Joomla development, HTML5, CSS3 and PHP.

Nohman Habib

CEO: codingace.com

Request a Quote









PHP Code Snippets Powered By : XYZScripts.com