- 5 years ago
- Zaid Bin Khalid
- 4,171 Views
-
4
Like most programming languages, PHP likewise permits you to compose code that performs various activities dependent on the consequences of a coherent or near test conditions at run time. This implies you can make test conditions as articulations that assess to either evident or bogus and dependent on these outcomes you can play out specific activities.
There are a few proclamations in PHP that you can use to decide.
- The if explanation.
- The if…else explanation.
- The if…elseif….else explanation.
- The switch and case explanation.
We will investigate every one of these announcements in the coming areas.
The if Statement
The if explanation is utilized to execute a square of code just if the predetermined condition assesses to genuine. This is the least complex PHP’s contingent explanations and can be composed like:
if(condition){
// Code to be executed
}
As should be obvious the later grammar is increasingly minimal and simple to compose.
<?php
$d = 1;
if($d == "1"){
echo "Your condition here!";
}
?>
The if & else Statement.
You can compare your result by adding an else condition with the IF statement. It works like just you read the example is given below. There is no return type of if and else it is just used in with condition.
if(condition){
// Code to be executed if condition is true
} else{
// Code to be executed if condition is false
}
Let’s see how the condition is working. Consider your self you want to display success text when your given condition is true otherwise you want to display an error.
<?php
$d = date('D');
if(strtolower($d) == "sun"){
echo "Your given condition is true";
} else{
echo "Today is not SUNDAY";
}
?>
The if elseif and else example.
The if-elseif-else an uncommon proclamation that is utilized to join different if-else explanations.
if(condition1){
// Code to be executed if condition1 is true
} elseif(condition2){
// Code to be executed if the condition1 is false and condition2 is true
} else{
// Code to be executed if both condition1 and condition2 are false
}
The accompanying model will yield “Have a pleasant end of the week!” if the present day is Friday, and “Have a decent Sunday!” if the present day is Sunday, else it will yield “Have a decent day!”
<?php
$d = date("D");
if($d == "Fri"){
echo "Have a nice weekend!";
} elseif($d == "Sun"){
echo "Have a nice Sunday!";
} else{
echo "Have a nice day!" . ($d);
}
?>
The Ternary Operator
The ternary administrator gives a shorthand method for composing the if and else articulations. The ternary administrator is spoken to by the question mark (?) image and it takes three operands. a condition to check, an outcome for genuine, and an outcome for bogus.
To see how this administrator functions, think about the accompanying models. The syntax is given below with an example.
(Condition) ? (Statement1) : (Statement2);
- Condition: First, the condition is the expression to be evaluated which returns a boolean value True or False.
- Statement 1: This statement runs when the first condition is True.
- Statement 2: This statement runs when the second condition is False.
<?php
if($age < 18){
echo 'Child'; // Display Child if age is less than 18
} else{
echo 'Adult'; // Display Adult if age is greater than or equal to 18
}
?>
The above if-else can be written with the help of a Ternary Operator like below.
<?php echo ($age<18)?'Child':'Adult'; ?>
Learn what is the difference between if-elseif-else and switch-case statements in PHP and how to use switch and case instead of if and else. Click here to see the complete detail with examples.
- 5 years ago
- Zaid Bin Khalid
- 4,171 Views
-
4