Warning: Trying to access array offset on value of type bool in /home/mukesh88/public_html/www.phpkida.com/wp-content/themes/blog-2020/templates/php.php on line 40

PHP Decision Making

PHP Decision making statements are used to perform different actions based on different conditions. You can use conditional statements in your php code to make your decisions. PHP supports following decision making statements:

  • if statement – if statement use to execute some code if a specified condition is true.
  • if…else statement – if statement use to execute some code if a condition is true and
    else code execute when if condition is false.
  • if…elseif….else statement – if…elseif….else statement use to select one of several blocks of code to be executed.
  • switch statement – switch statement use to select one of many blocks of code to be executed.

let’s we start working with PHP Decision making statement using some examples for better understanding.

if statement

if statement use to execute some code if a specified condition is true.

Syntax of if statement

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

example of if statement –

<?php
$num=10;
if ($num==10)
{
echo "Variable is equal to 10";
}
?>

if…else statement

if statement use to execute some code if a condition is true and else code execute when if condition is false

Syntax of if…else statement –

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

Example of if…else statement

<?php

$num=10;
if ($num==10)
{
echo "Variable is equal to 10!";
}
else
{
echo "Variable is not equal to 10!";
}
?>

if…elseif….else statement

if…elseif….else statement use to select one of several blocks of code to be executed.

Syntax of if…elseif….else statement

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 of if…elseif….else statement

<?php

$d=date("D");

if ($d=="Fri")
{
echo "Have a nice weekend!";
}

elseif ($d=="Sun")
{
echo "Have a nice Sunday!";
}

else
{
echo "Have a nice day!";
}
?>

Nested if condition

in this condition within condition, first execute first condition then execute inside condition if one condition if false then inside condition will be not executed.

Syntax of Nested if condition

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

}
else
{ code to be executed if condition is false;}
}

else { code to be executed if condition is false; }

Example of Nested if condition

<?php
$email="xyz@phpkida.com";
$pass="xyzabc%9";
if ($email!="")
{
if($pass!="")
{
if($email=="xyz@phpkida.com" && $pass=="xyzabc%9")
{
echo "You Login Successfull !";
}
else
{ echo "Your email id or password is wrong !";}

}
else
{ echo "Please enter your password first !";}
}

else { echo "please enter email id first !; }

The Switch Statement

If you want to select one of many blocks of code to be executed, use the Switch statement.
The switch statement is used to avoid long blocks of if..elseif..else code.

Syntax of switch statement

switch (expression)
{
case label1:
code to be executed if expression = label1;
break;
case label2:
code to be executed if expression = label2;
break;
default:
code to be executed
if expression is different
from both label1 and label2;

}

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

Sign up for weekly update

Milkshake is almost ready. If you're interested in testing it out, then sign up below to get exclusive access.