- if statement - executes some code only if a specified condition is true
- if...else statement - executes some code if a condition is true and another code if the condition is false
- if...elseif....else statement - selects one of several blocks of code to be executed
PHP - The if Statement
The if statement is used to execute some code only if a specified condition is true.
Syntax
if (condition) {
code to be executed if condition is true;
}
code to be executed if condition is true;
}
Example
<?php$text = 'Something';
if($text=='Something else'){
echo 'TRUE';
}else{
echo 'FALSE';
}
Output:
FALSE
PHP - The if...else if Statement
Use the if....else statement to execute some code if a condition is true and another code if the
condition is false.
The example below will output "Have a good day!" if the current time is less
than 20, and "Have a good night!" otherwise:
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
$number = 11;
if($number==10){
echo 'Equal to 10';
}else if ($number==11){
echo 'Equal to 11';
}else{
echo 'Not equal';
}
?>
$number = 11;
if($number==10){
echo 'Equal to 10';
}else if ($number==11){
echo 'Equal to 11';
}else{
echo 'Not equal';
}
?>
Output:Equal to 11
No comments:
Post a Comment