How To Use Php Conditional Statement Example

PHP 8 , PHP

Nicesnippets

887

17-06-2021


How To Use Php Conditional Statement Example

Hi guys,

Today i will explained how to use php conditional statement in your php file. This example is so easy to use in php. This example to i am use in many conditional statement in php file.

THe conditional statement is provided in a php in default. Conditional statement use to vary easy here.

THis example to i am use in if condition,if..else condition,if...elseif...else conditon and last to use in switch case conditional statement use.So you are follow for my step by step.

So let's start to the example.

If Condition


The if condition is working to execute in only true the condition in php.

<?php

$t = 120;

if ($t == "120") {

echo "Have a good day!<br>";

}

?>

If..else Condition

If..else condition is work to the true or false part. The condition is true then execute the if statement and condition is false then execute in false part.

<?php

$t = 120;

if ($t == "20") {

echo "Have a good day!<br>";

} else {

echo "Have a good night!<br>";

}

?>

if...elseif...else Condition

if...elseif...else condition statement executes different codes for more then two conditions

<?php

$t = 120;

if ($t != "120") {

echo "Have a good morning!<br>";

} elseif ($t == "120") {

echo "Have a good day!<br>";

} else {

echo "Have a good night!<br>";

}

?>

Switch..case Conditional

Switch statement is used to perform different actions code on different conditions. The switch statement to select one of many blocks of code to executed.

<?php

$favcolor = "blue";

switch ($favcolor) {

case "red":

echo "Your favorite color is red!<br>";

break;

case "blue":

echo "Your favorite color is blue!<br>";

break;

case "green":

echo "Your favorite color is green!<br>";

break;

default:

echo "Your favorite color is neither red, blue, nor green!<br>";

}

?>

now you can check your own.

i hope it can help you...


Recommended Posts