PHP Conditions

Statements like if, elseif,…else, and switch are used to make decisions based on different conditions.

You can decide what to do in your code with the help of \”if\” statements. PHP supports the following three statements for making decisions: −
Statements that help PHP decide what to do.

PHP statement

Use the if…else statement if you want to run one set of code when a condition is true and another set of code when the condition is not true.

The elseif statement is used with the if…else statement to run a set of code if one of several conditions is true.

If you want to choose which of several blocks of code to run, you can use the Switch statement. The switch statement is used to get rid of long blocks of if..elseif..else code.

If…else Statement

Use the if…else statement to run one set of code if a condition is true and another set of code if the condition is false.

Syntax

if (condition)

code to be executed if condition is true;

else

code to be executed if condition is false;

 

Example

The output of the following code will be \”Have a great weekend!\” if today is Saturday, it will say \”Have a great day!\”; otherwise, it will say \”Have a nice day!\”:

<html>
<body>

<?php
$d = date(\”D\”);

if ($d == \”Sat\”)
echo \”Have a great weekend!\”;

else
echo \”Have a great day!\”;
?>

</body>
</html>

Output −

Have a nice weekend!

The ElseIf Condition

Use the elseif statement if you want to run some code only if one of several conditions is met.

Syntax

if (condition)
code execute if condition is true;
elseif (condition)
code execute if condition is true;
else
code execute if condition is false;

Example

If the current day is Saturday, the following code will say \”Have a great weekend!\” and if the current day is Sunday, it will say \”Enjoy your  Sunday!\” If not, it will say \”Have a good day!\”

<html>
<body>

<?php
$dat = date(\”D\”);

if ($dat == \”Sat\”)
echo \”Have a great weekend!\”;

elseif ($dat == \”Sun\”)
echo \”Enjoy your  Sunday!\”;

else
echo \”Have a good day!\”;
?>

</body>
</html>

Output −

Have a nice Weekend!

Switch Statement

Switch statement is used to choose which of several blocks of code to run.

With the switch statement, you can avoid long blocks of if..elseif..else code.

Syntax

switch (expression){
case condition1:
code execute if expression = condition1;
break;

case condition2:
code to be executed if expression = condition2;
break;

default:
code to be executed
if expression is different

from both condition1 and condition2;
}

Example

The way the switch statement works is strange. First, it looks for a label that matches the value of the expression. If a matching value is found, the code for the matching label will be run. If no matching label is found, the then statement will run any default code that was set.

 

<html>
<body>

<?php
$dat = date(\”D\”);

switch ($dat){
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 day is this\”;
}
?>

</body>
</html>

Output −

Today is Saturday

Related Posts
50+ PHP Interview Questions and Answers 2023

1. Differentiate between static and dynamic websites. Static Website The content cannot be modified after the script is executed The Read more

All We Need to Know About PHP Ecommerce Development

  Many e-commerce sites let you search for products, show them off, and sell them online. The flood of money Read more

PHP Custom Web Development: How It Can Be Used, What Its Pros and Cons Are,

PHP is a scripting language that runs on the server. It uses server resources to process outputs. It is a Read more

PHP Tutorial

Hypertext Preprocessor (PHP) is a programming language that lets web developers make dynamic content that works with databases. PHP is Read more

Introduction of PHP

PHP started out as a small open source project that grew as more and more people found out how useful Read more

Syntax Overview of PHP

This chapter will show you some of PHP\'s very basic syntax, which is very important for building a strong PHP Read more

Environment Setup in PHP

To develop and run PHP on your computer, you need to instal three important parts. Web server PHP can almost Read more

Variable Types in PHP

Using a variable is the main way to store information in the middle of a PHP program. Here are the Read more

Scroll to Top