2018-10-04 13:47:55 +00:00
---
title: If-else Statement
---
## Introduction
2018-10-16 03:59:38 +00:00
If/Else is a conditional statement where depending on the truthiness of a condition, different actions will be performed.
2018-10-04 13:47:55 +00:00
2018-10-16 03:59:38 +00: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 13:47:55 +00:00
## If Statement
2018-10-16 03:59:38 +00:00
```
< ?php
if (condition) {
2018-10-04 13:47:55 +00:00
statement1;
statement2;
}
2018-10-16 03:59:38 +00:00
```
2018-12-13 08:51:39 +00: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 13:47:55 +00:00
## If/Else Statement
2018-10-16 03:59:38 +00:00
```
< ?php
if (condition) {
2018-10-04 13:47:55 +00:00
statement1;
statement2;
2018-10-16 03:59:38 +00:00
} else {
2018-10-04 13:47:55 +00:00
statement3;
statement4;
}
2018-10-16 03:59:38 +00:00
```
2018-12-13 08:51:39 +00:00
> **Note:** The `else` statement is optional.
2018-10-04 13:47:55 +00:00
## If/Elseif/Else Statement
2018-10-16 03:59:38 +00:00
```
< ?php
if (condition1) {
2018-10-04 13:47:55 +00:00
statement1;
statement2;
2018-10-16 03:59:38 +00:00
} elseif (condition2) {
2018-10-04 13:47:55 +00:00
statement3;
statement4;
2018-10-16 03:59:38 +00:00
} else {
2018-10-04 13:47:55 +00:00
statement5;
2018-10-16 03:59:38 +00:00
}
```
2018-12-13 08:51:39 +00:00
> **Note:** `elseif` should always be written as one word.
2018-10-14 16:10:03 +00:00
## Nested If/Else Statement
2018-10-16 03:59:38 +00:00
```
< ?php
if (condition1) {
if (condition2) {
2018-10-14 16:10:03 +00:00
statement1;
statement2;
2018-10-16 03:59:38 +00:00
} else {
2018-10-14 16:10:03 +00:00
statement3;
statement4;
}
2018-10-16 03:59:38 +00:00
} else {
if (condition3) {
2018-10-14 16:10:03 +00:00
statement5;
statement6;
2018-10-16 03:59:38 +00:00
} else {
2018-10-14 16:10:03 +00:00
statement7;
statement8;
}
}
2018-10-16 03:59:38 +00: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 08:51:39 +00: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-16 03:59:38 +00:00
## Ternary Operators
2018-12-13 08:31:49 +00:00
Another important option to consider when using short If/Else statements is the ternary operator.
2018-10-04 13:47:55 +00:00
2018-12-13 08:31:49 +00: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 08:31:49 +00:00
```php
2018-10-16 04:27:58 +00:00
if (condition1):
statement1;
2018-12-13 08:40:56 +00:00
else:
2018-10-16 04:27:58 +00:00
statement5;
2018-12-13 08:40:56 +00:00
endif;
2018-12-13 09:02:32 +00:00
```
2018-10-16 04:27:58 +00:00
2018-12-13 09:02:32 +00:00
#### 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 >