PHP Conditional Statments

PHP Conditional Statments

Hope this article find you well.

Today we will learn about the conditional statement available in PHP.

So read the article till the end and i will make sure you know everything about PHP conditional statments.

Before i proceed, i would request you to do SUBSCRIBE to ShoutMe360.com so that you can get regular updates.

So lets start…..

What are the Conditional Statements?

Conditional statements are the set of instructions used to perform different actions based on different conditions.

Most of the times when a developer writes code, he/she wants to perform different actions for different conditions. In that case conditional statements come into use.

Below are the conditional statements available in PHP:

In PHP we have the following conditional statements:

if statement – use this statement when you need to execute a code if one condition is true.
if…else statement – use this statement when you need to execute some code if a condition is true and another code if that condition is false.
if…elseif….else statement – use this statement when different codes for more than two conditions
switch statement – is used when you have to select one of many blocks of code to be executed. It can be also used as alternative of if..elseif..else code.

The if Statement

If you want to execute a code if one condition is true.

Syntax
if (condition)
         code to be executed if condition is true;

Example
The following example will output “Legends born in June!” if the current month is June.

<?php
$d = date(“M”);

if ($d == “Jun”)
echo “Legends born in June!”;

?>

The if…else Statement

If you want to execute some code if a condition is true and another code if that condition is false.

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

Example
The following example will output “Have a good day!” if the current hour is less than 20, Otherwise, it will output “Have a good night!“:

<?php
$t = date(“H”);
if ($t < “20”)
{
        echo “Have a good day!”;
}
else
{
        echo “Have a good night!”;
}
?>

The if…elseif….else Statement

Use this statement when different codes for more than two conditions

if (condition)
     code to be executed if condition is true;
elseif (condition)
     code to be executed if condition is true;
else
     code to be executed if condition is false;

Example
The example below will output “Have a good morning!” if the current time is less than 12, and “Have a good day!” if the current time is less than 20. Otherwise it will output “Have a good night!”:

<?php

$t = date(“H”);

if ($t < “12”) {
       echo “Have a good morning!”;
}
elseif ($t < “20”)
{
      echo “Have a good day!”;
}
else
{
       echo “Have a good night!”;
}
?>

The switch Statement
If you want to select one of many blocks of code to be executed, use the Switch statement.

<?php
$d = date(“D”);

switch ($d){
case “Mon”:
echo “Today is Monday”;
break;

case “Tue”:
echo “Today is Tuesday”;
break;

case “Wed”:
echo “Today is Wednesday”;
break;

case “Thu”:
echo “Today is Thursday”;
break;

case “Fri”:
echo “Today is Friday”;
break;

case “Sat”:
echo “Today is Saturday”;
break;

case “Sun”:
echo “Today is Sunday”;
break;

default:
echo “Wonder which day is this ?”;
}
?>

Hope this explains you enough about the Conditional Statements.

This is Lalit signing off for today.

If you like this post please give a thumbs up and keep it sharing.

Hey don’t forget to SUBSCRIBE please so that you can get notification on new posts.

Tada

Leave a Reply