PHP Programing language

Decision Making in PHP
Previous Home Next

Decision Making

Decision Making Statement is very powerful statement in PHP. It is used in Php to take some decision in PHP Script. It also perform some tasks based on conditinat the time of decision making statements are used.

Types of Decision Making Statement:

The following Decision Making Statements support by PHP

  1. if Statement
  2. if....else Statement
  3. elseif Statement
  4. Switch....Case Statement
if Statement

if Statement is used to execute the statement when only condition is true.

Syntax:
if(condition)
{
Statement1; //if condition is true
}

 

Example:

<html>
<head>
<title>if Statement in PHP</title>
</head>
<body>

<?php
$A=58;
If($A % 2 ==0)
{
echo "$A is Even";
}
?>

</body>
</html>

Output:

if....else Statement

if....else Statement is used to execute the statement when condition is true and execute anorher statement when condition is false.

Syntax:
if(condition)
{
Statement1;  //if condition is true
}
else
{
Statement2;  //if statement1 false
}

Example:

<html>
<head>
<title>if....else Statement in PHP</title>
</head>
<body>

<?php
$A=57;
If($A % 2 ==0)
{
echo "$A is Even";
}
else
{
echo "$A is Odd";
}
?>

</body>
</html>

Output:

elseif Statement

if there are more than one true statemment then there is a use of elseif statement.

Syntax:
if(condition)
{
Statement1;   //code executed when condition is true;
}
elseif(condition)
{
Statemment2;  //code executed when condition is true
}
else
{
Statement3;  //code executed when condition is false
}

Example:

<html>
<head>
<title>elseif Statement in PHP</title>
</head>
<body>

<?php
$A = 39;
echo "Marks is: $A<br/>";
if($A <= 99 && $A >= 70)
{
echo "Result: You got first Position";
}
elseif($A <= 70 && $A >= 55)
{
echo "Result: You got second Position";
}
elseif($A <= 55 && $A >= 40)
{
echo "Result: You got third Position";
}
else
{
echo "Result: Fail";
}
?>

</body>
</html>

Output:

Switch....Case Statement

Switch Statement is used if you want to choose one of many blocks of code to be executed. The switch statement is used to avoid long blocks of if..elseif..else code.

Syntax:
switch (expression)
{
Case 1 :
Sttement1;
break;

Case 2 :
Statement2;
break;
..........
..........
..........
default :
Default Statement;
break;
}

Example:

<html>
<head>
<title>Switch....Case Statement in PHP</title>
</head>
<body>

<?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 "Invalid Day.....!!!!!";
}
?>

</body>
</html>

Output:

Previous Home Next

Tolal:0 Click:

Show All Comments

Did not find what you were looking for leave your name and message. We will revert within 24 hours
Name:
eMail:
Comment / Feedback: