2018-10-04 14:47:55 +01:00
---
title: If-else Statement
---
## Introduction
2018-10-15 23:59:38 -04:00
If/Else is a conditional statement where depending on the truthiness of a condition, different actions will be performed.
2018-10-04 14:47:55 +01:00
2018-10-15 23:59:38 -04:00
> **Note:** The `{}` brackets are only needed if the condition has more than one action statement; however, it is best practice to include them regardless.
2018-10-04 14:47:55 +01:00
## If Statement
2018-10-15 23:59:38 -04:00
```
< ?php
if (condition) {
2018-10-04 14:47:55 +01:00
statement1;
statement2;
}
2018-10-15 23:59:38 -04:00
```
2018-12-13 03:51:39 -05:00
> **Note:** You can nest as many statements in an "if" block as you'd like; you are not limited to the amount in the examples.
2018-10-04 14:47:55 +01:00
## If/Else Statement
2018-10-15 23:59:38 -04:00
```
< ?php
if (condition) {
2018-10-04 14:47:55 +01:00
statement1;
statement2;
2018-10-15 23:59:38 -04:00
} else {
2018-10-04 14:47:55 +01:00
statement3;
statement4;
}
2018-10-15 23:59:38 -04:00
```
2018-12-13 03:51:39 -05:00
> **Note:** The `else` statement is optional.
2018-10-04 14:47:55 +01:00
## If/Elseif/Else Statement
2018-10-15 23:59:38 -04:00
```
< ?php
if (condition1) {
2018-10-04 14:47:55 +01:00
statement1;
statement2;
2018-10-15 23:59:38 -04:00
} elseif (condition2) {
2018-10-04 14:47:55 +01:00
statement3;
statement4;
2018-10-15 23:59:38 -04:00
} else {
2018-10-04 14:47:55 +01:00
statement5;
2018-10-15 23:59:38 -04:00
}
```
2018-12-13 03:51:39 -05:00
> **Note:** `elseif` should always be written as one word.
2018-10-14 21:40:03 +05:30
## Nested If/Else Statement
2018-10-15 23:59:38 -04:00
```
< ?php
if (condition1) {
if (condition2) {
2018-10-14 21:40:03 +05:30
statement1;
statement2;
2018-10-15 23:59:38 -04:00
} else {
2018-10-14 21:40:03 +05:30
statement3;
statement4;
}
2018-10-15 23:59:38 -04:00
} else {
if (condition3) {
2018-10-14 21:40:03 +05:30
statement5;
statement6;
2018-10-15 23:59:38 -04:00
} else {
2018-10-14 21:40:03 +05:30
statement7;
statement8;
}
}
2018-10-15 23:59:38 -04:00
```
## Multiple Conditions
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!';
}
```
2018-12-13 03:51:39 -05:00
> **Note:** It's a good practice to wrap individual conditions in parens when you have more than one (it can improve readability).
2018-10-15 23:59:38 -04:00
## Ternary Operators
2018-12-13 02:31:49 -06:00
Another important option to consider when using short If/Else statements is the ternary operator.
2018-10-04 14:47:55 +01:00
2018-12-13 02:31:49 -06:00
```php
$statement=(condition1 ? "condition1 is true" : "condition1 is false");
2018-12-13 08:20:36 +00:00
```
## Alternative If/Else Syntax
There is also an alternative syntax for control structures
2018-12-13 02:31:49 -06:00
```php
2018-10-16 10:27:58 +06:00
if (condition1):
statement1;
2018-12-13 10:40:56 +02:00
else:
2018-10-16 10:27:58 +06:00
statement5;
2018-12-13 10:40:56 +02:00
endif;
2018-12-13 14:32:32 +05:30
```
2018-10-16 10:27:58 +06:00
2018-12-13 14:32:32 +05:30
#### More Information:
* < a href = 'http://php.net/manual/en/control-structures.alternative-syntax.php' target = '_blank' rel = 'nofollow' > PHP Alternative syntax for control structures</ a >
* < a href = "http://php.net/manual/en/control-structures.if.php" rel = "nofollow" > php.net control structures If Manual</ a >
* < a href = "https://secure.php.net/manual/en/control-structures.elseif.php" rel = "nofollow" > php.net control structures Else If Manual</ a >