2018-10-04 14:47:55 +01:00
|
|
|
---
|
|
|
|
title: If-else Statement
|
|
|
|
---
|
|
|
|
## Introduction
|
|
|
|
If/Else is a conditional statement where depending on the truthiness of a condition, diffierent actions will be performed.
|
|
|
|
|
|
|
|
> **Note:** The `{}` brackets are only needed if the condition has more than one action statement.
|
|
|
|
|
|
|
|
## If Statement
|
|
|
|
~~~~
|
|
|
|
if (condition){
|
|
|
|
statement1;
|
|
|
|
statement2;
|
|
|
|
}
|
|
|
|
~~~~
|
|
|
|
> **Note:** The `else` statement is optional.
|
|
|
|
## If/Else Statement
|
|
|
|
~~~~
|
|
|
|
if (condition){
|
|
|
|
statement1;
|
|
|
|
statement2;
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
statement3;
|
|
|
|
statement4;
|
|
|
|
}
|
|
|
|
~~~~
|
|
|
|
## If/Elseif/Else Statement
|
|
|
|
~~~~
|
2018-10-14 21:40:03 +05:30
|
|
|
if (condition1){
|
2018-10-04 14:47:55 +01:00
|
|
|
statement1;
|
|
|
|
statement2;
|
|
|
|
}
|
2018-10-14 21:40:03 +05:30
|
|
|
elseif (condition2){
|
2018-10-04 14:47:55 +01:00
|
|
|
statement3;
|
|
|
|
statement4;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
statement5;
|
|
|
|
~~~~
|
2018-10-14 21:40:03 +05:30
|
|
|
## Nested If/Else Statement
|
|
|
|
~~~~
|
|
|
|
if (condition1){
|
|
|
|
if (condition2){
|
|
|
|
statement1;
|
|
|
|
statement2;
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
statement3;
|
|
|
|
statement4;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (condition3){
|
|
|
|
statement5;
|
|
|
|
statement6;
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
statement7;
|
|
|
|
statement8;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
~~~~
|
2018-10-04 14:47:55 +01:00
|
|
|
|
|
|
|
For more information check out the following link:
|
|
|
|
<a href='http://php.net/manual/en/control-structures.elseif.php' target='_blank' rel='nofollow'>PHP Else</a>
|