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-10-04 14:47:55 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								>  **Note:** The `else` statement is optional.
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								## 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 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								>  **Note:** `elseif` should always be written as one word.
  
						 
					
						
							
								
									
										
										
										
											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-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!';
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								## Ternary Operators
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								Another important option to consider when using short If/Else statements is the ternary operator.
							 
						 
					
						
							
								
									
										
										
										
											2018-10-04 14:47:55 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-10-16 10:27:58 +06:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								Also there is an alternative syntax for control structures
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								~~~~
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  if (condition1):
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    statement1;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  endif;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  else
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    statement5;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								~~~~
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								For more information check out the following link:
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								< a  href = 'http://php.net/manual/en/control-structures.alternative-syntax.php'  target = '_blank'  rel = 'nofollow' > PHP Alternative syntax for control structures< / a >  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-10-15 23:59:38 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								For more information please check out the following link:
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								[PHP: if ](http://php.net/manual/en/control-structures.if.php )