php decision making

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

Tools:

Crypto Currency

PHPKIDA provides free online the best real-time crypto market price, news, predictions, index with graphs & historical data for 2000+ coins including bitcoin, ethereum, & altcoins from major cryptocurrency exchanges.

Visite Tool

Icon Generator

PHPKIDA provides free online icon generator. Our icon generator help you to edit, modify, resize and customize icon for your website and applications. Convert to a shap you like add icon border, you can change icon border color, border size, background color, icon shadow, icons color, icon size in your own way and save it as a png image.

Visite Tool

Youtube Video Downloader

PHPKIDA provides Free online YouTube video downloader to download YouTube videos quickly in MP4, 3GP, and more. Search your video and free fast download youtube videos.

Visite Tool

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.