Learn Php In 24 Hours Part Iii Module I
PHP

Learn PHP in just 24 hours – Part III | Module I

PHP Decision Making Statements (Conditional Statements)

The if, elseif ….else, and switch statements are used to take decision based on different condition. We can use conditional statements in our program to make our decisions.

PHP supports following conditional statements.

  1. If … Else Statement

If you want to execute some code if a condition is true and other code if the condition is false, then you have to use the If …Else Statement.

Syntax:

if(condition)

statements;

else

statements;

For Example:

<?php

$day = “Friday”;

if($day == “Friday”)

echo “Have a nice weekend”;

else

echo “Have a nice day”;

?>

Output:

Have a nice weekend

  1. Elseif Statement

If you want to test several conditions and want to execute some code based on those conditions then Elseif statement is useful.

Syntax:

if(condition)

statements;

elseif(condition)

statements;

else

statements;

For Example:

<?php

$day = “Friday”;

if($day == “Friday”)

echo “Have a nice weekend”;

elseif($day == “Sunday”)

echo “Have a nice Sunday”;

else

echo “Have a nice day!”;

?>

  1. Switch Statement

If you want to select block of code if the certain condition is true then you use the switch case statement. It is quite similar if else statement but it avoids long block of if…else if….else program.

Syntax:

switch(expression)

{

case label1:

statement1;

Break;

case label2:

statement2;

break;

default:

statement3;

//code to be executed if the expression is different from label1 and label2

}

Example:

<?php

$today = “Sunday”;

switch($today)

{

case “Sunday”:

echo “Today is Sunday”;

break;

case “Monday”:

echo “Today is Monday”;

break;

case “Tuesday”:

echo “Today is Tuesday”;

break;

case “Wednesday”:

echo “Today is Wednesday”;

break;

case “Thursday”:

echo “Today is Thursday”;

break;

case “Friday”:

echo “Today is Friday”;

break;

case “Saturday”:

echo “Today is Saturday”;

break;

default:

echo “Invalid Day”;

break;

}

<?php

 

Summary :

Decision making statements or conditional statements are statements which are used take decision based on conditions. In real life, We have to take lots of decisions based on conditions, likewise in computer programming, we take decisions based on conditions.

Decision making statements are used to control the execution of the program.

 

I hope you understood the concept of Decision making statements very well. If you have any confusion points or questions regarding this, please comment your question below

 

 

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.