PHP, during a program, the programmer can evaluate different conditions and decide on these conditions based on the evaluation of the truth of false..
Different types of conditional statements
The if Statement
In if Statements Output will appear when only Condition must be true.
Syntax -
if (condition) {
code to be executed if condition is true;
}
code to be executed if condition is true;
}
Example -
<?php
$t = date("H");
if ($t < "20") {
echo "Have a good day!";
}
?>
$t = date("H");
if ($t < "20") {
echo "Have a good day!";
}
?>
The if-else Statement
If-else statement allows you to display output in both the condition (if the status is displayed, some messages display another message)
Syntax-
if (condition) {
code to be executed if condition is true;
} else {
code to be executed if condition is false;}
code to be executed if condition is true;
} else {
code to be executed if condition is false;}
Example -
<?php
$t = date("H");
if ($t < "20") {
echo "Have a good day!";
} else {
echo "Have a good night!";
}
?>
$t = date("H");
if ($t < "20") {
echo "Have a good day!";
} else {
echo "Have a good night!";
}
?>
The if-elseif-else Statement
If-else-if-else statement allows you to series together with multiple e-statements, thus allowing the programmer to define actions for more than two possible outcomes.
Syntax -
if (condition) {
code to be executed if this condition is true;} elseif (condition) {
code to be executed if this condition is true;} else {
code to be executed if all conditions are false;}
code to be executed if this condition is true;} elseif (condition) {
code to be executed if this condition is true;} else {
code to be executed if all conditions are false;}
Example -
<?php
$t = date("H");
if ($t < "10") {
echo "Have a good morning!";
} elseif ($t < "20") {
echo "Have a good day!";
} else {
echo "Have a good night!";
}
?>
$t = date("H");
if ($t < "10") {
echo "Have a good morning!";
} elseif ($t < "20") {
echo "Have a good day!";
} else {
echo "Have a good night!";
}
?>
The switch Statement
The switch statement is similar to a series of if statements on the same expression.
Syntax -
switch (n) {
case label1:
code to be executed if n=label1;
break;
case label2:
code to be executed if n=label2;
break;
case label3:
code to be executed if n=label3;
break;
...
default:
code to be executed if n is different from all labels;
}
case label1:
code to be executed if n=label1;
break;
case label2:
code to be executed if n=label2;
break;
case label3:
code to be executed if n=label3;
break;
...
default:
code to be executed if n is different from all labels;
}
Example-
<?php
$favcolor = "red";
switch ($favcolor) {
case "red":
echo "Your favorite color is red!";
break;
case "blue":
echo "Your favorite color is blue!";
break;
case "green":
echo "Your favorite color is green!";
break;
default:
echo "Your favorite color is neither red, blue, nor green!";
}
?>
$favcolor = "red";
switch ($favcolor) {
case "red":
echo "Your favorite color is red!";
break;
case "blue":
echo "Your favorite color is blue!";
break;
case "green":
echo "Your favorite color is green!";
break;
default:
echo "Your favorite color is neither red, blue, nor green!";
}
?>
0 comments:
Post a Comment