What
is a Conditional or decisions making
statement in php ?
Conditional statements are used to
perform different actions based on different conditions.
You can use conditional
statements in your code to make your decisions.
Conditional statements are extremely useful if you want to make your
scripts dynamic, i.e.: making them react to a form submission or a
certain value being applied to a specific variable.
What
are the types of Conditional or decisions
making statement in php ?
In PHP there are generally these following
conditional statements:
- if statement - executes some
code if one condition is true
- if...else statement - executes some
code if a condition is true and another code if that condition is false
- if...elseif...else statement
- executes different codes for more than two conditions
- switch statement - selects one of
many blocks of code to be executed
1.The
if Statement
If you want to execute some code
if a condition is true , then you can use the if statement.
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!";
}
?>
2.The
if...else Statement
The if...else statement executes some code if a condition is true and another code if that condition is false.
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!";
}
?>
3.if...elseif....else Statement
The if...elseif...else statement executes different codes for more than two conditions.
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!";
}
?>
4.Switch
Statement
The switch statement is used to select one of many blocks
of code to be executed.
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!";
}
?>