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

PHPKIDA Offering Free Tools:

INSTA DOWNLOADER

Free online tool to download Instagram videos, photos and save Instagram videos, photos to your device. It's free & easy to use.

Download Now

CURRENCY CONVERTER

Our real time currency converter calculator will convert your money based on current values from around the world. It's free & easy to use.

Convert Now

ROBOST.TXT GEN.

Create robots txt file that help search engines to crawling and indexing your site with help of our free online Robots.txt Generator tool.

Create Now

HTML MINIFIER

Free online tool to minify or compress html code and reduce the size of html code in few clicks. It's free & easy to use.

Minify Now

CSS MINIFIER

Free online tool to minify or compress css code and reduce the size of css code in few clicks. It's free & easy to use.

Minify Now

JAVASCRIPT MINIFIER

Free online tool to minify or compress javascript code and reduce the size of javascript code in few clicks. It's free & easy to use.

Minify Now

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.