Revised formatting and added information (#19431)
Revised formatting to follow PSR, included additional examples, added multiple conditions with logical operators, and added a reference to ternary operators.
This commit is contained in:
committed by
Randell Dawson
parent
c8de747d0e
commit
37318fa8da
@ -2,65 +2,95 @@
|
|||||||
title: If-else Statement
|
title: If-else Statement
|
||||||
---
|
---
|
||||||
## Introduction
|
## Introduction
|
||||||
If/Else is a conditional statement where depending on the truthiness of a condition, diffierent actions will be performed.
|
If/Else is a conditional statement where depending on the truthiness of a condition, different actions will be performed.
|
||||||
|
|
||||||
> **Note:** The `{}` brackets are only needed if the condition has more than one action statement.
|
> **Note:** The `{}` brackets are only needed if the condition has more than one action statement; however, it is best practice to include them regardless.
|
||||||
|
|
||||||
## If Statement
|
## If Statement
|
||||||
~~~~
|
|
||||||
|
```
|
||||||
|
<?php
|
||||||
|
|
||||||
if (condition) {
|
if (condition) {
|
||||||
statement1;
|
statement1;
|
||||||
statement2;
|
statement2;
|
||||||
}
|
}
|
||||||
~~~~
|
```
|
||||||
> **Note:** The `else` statement is optional.
|
> **Note:** The `else` statement is optional.
|
||||||
## If/Else Statement
|
## If/Else Statement
|
||||||
~~~~
|
|
||||||
|
```
|
||||||
|
<?php
|
||||||
|
|
||||||
if (condition) {
|
if (condition) {
|
||||||
statement1;
|
statement1;
|
||||||
statement2;
|
statement2;
|
||||||
}
|
} else {
|
||||||
else{
|
|
||||||
statement3;
|
statement3;
|
||||||
statement4;
|
statement4;
|
||||||
}
|
}
|
||||||
~~~~
|
```
|
||||||
|
> **Note:** `elseif` should always be written as one word.
|
||||||
## If/Elseif/Else Statement
|
## If/Elseif/Else Statement
|
||||||
~~~~
|
|
||||||
|
```
|
||||||
|
<?php
|
||||||
|
|
||||||
if (condition1) {
|
if (condition1) {
|
||||||
statement1;
|
statement1;
|
||||||
statement2;
|
statement2;
|
||||||
}
|
} elseif (condition2) {
|
||||||
elseif (condition2){
|
|
||||||
statement3;
|
statement3;
|
||||||
statement4;
|
statement4;
|
||||||
}
|
} else {
|
||||||
else
|
|
||||||
statement5;
|
statement5;
|
||||||
~~~~
|
}
|
||||||
|
```
|
||||||
## Nested If/Else Statement
|
## Nested If/Else Statement
|
||||||
~~~~
|
|
||||||
|
```
|
||||||
|
<?php
|
||||||
|
|
||||||
if (condition1) {
|
if (condition1) {
|
||||||
if (condition2) {
|
if (condition2) {
|
||||||
statement1;
|
statement1;
|
||||||
statement2;
|
statement2;
|
||||||
}
|
} else {
|
||||||
else{
|
|
||||||
statement3;
|
statement3;
|
||||||
statement4;
|
statement4;
|
||||||
}
|
}
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
if (condition3) {
|
if (condition3) {
|
||||||
statement5;
|
statement5;
|
||||||
statement6;
|
statement6;
|
||||||
}
|
} else {
|
||||||
else{
|
|
||||||
statement7;
|
statement7;
|
||||||
statement8;
|
statement8;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
~~~~
|
```
|
||||||
|
|
||||||
For more information check out the following link:
|
## Multiple Conditions
|
||||||
<a href='http://php.net/manual/en/control-structures.elseif.php' target='_blank' rel='nofollow'>PHP Else</a>
|
|
||||||
|
Multiple conditions can be used at once with the "or" (||), "xor", and "and" (&&) logical operators.
|
||||||
|
|
||||||
|
For instance:
|
||||||
|
|
||||||
|
```
|
||||||
|
<?php
|
||||||
|
|
||||||
|
if (condition1 && condition2) {
|
||||||
|
echo 'Both conditions are true!';
|
||||||
|
} elseif (condition 1 || condition2) {
|
||||||
|
echo 'One condition is true!';
|
||||||
|
} else (condition1 xor condition2) {
|
||||||
|
echo 'One condition is true, and one condition is false!';
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
## Ternary Operators
|
||||||
|
|
||||||
|
Another important option to consider when using short If/Else statements is the ternary operator.
|
||||||
|
|
||||||
|
For more information please check out the following link:
|
||||||
|
[PHP: if](http://php.net/manual/en/control-structures.if.php)
|
||||||
|
Reference in New Issue
Block a user