By using this site you agree to the use of cookies and related technologies.

Learn more

PHP If Then Else and ElseIf Statements

How to work with If Statements in PHP. This Tutorial shows how to work with ifs, else ifs, shortened ifs.

Edited: 2016-09-24 01:22

If statements are conditional statements, which allows execution of fragments of code, all depending on what condition has been meet.

Basic if statements's are useful to check for conditions, such as, whether a user is logged in. You can also include so called else if's in statements, which allows you to run alternative code, in case the condition that you have set wasn't meet.

PHP If Example

In this example I'm going to use the rand function, to create a "random" number between 1 and 2.

If the number is 1, we will echo "The number was 1", and accordingly "The number was 2" if that was the case.

<?php
$RandomNumber = rand(1, 2);

if ($RandomNumber == '1') { // PHP If Statement
  echo 'The number was 1';
} else { // PHP Else
  echo 'The number was 2';
}
?>

All I'm really doing in above example, is checking if the number was "1", if it wasn't we can safely assume it was "2".

PHP else if

In the next example, you should try to include else if statements, to check for aditional conditions. Use the same approach as before, but this time use 4 numbers instead. So simply extend the if statement from before, to include one or more else if.

You should Keep in mind that, you don't really need to do a check for something, when you can predict the outcome. So we simply include a normal else at the end of the statement, which is faster then including another else if, since it php won't have to spend CPU checking if the condition was meet.

The else at the end, will be used to execute the the code intended to be run, if the number is "4".

<?php
$RandomNumber = rand(1, 4);

if ($RandomNumber == '1') { // PHP If Statement
  echo 'The number was 1';
} elseif ($RandomNumber == '2') { // PHP Elseif Statement
  echo 'The number was 2';
} elseif ($RandomNumber == '3') { // PHP Elseif Statement
  echo 'The number was 3';
} else {
  echo 'The number was 4'; // PHP Else
}
?>

This was primarily to show how if statements work, the above would be both easier and faster, simply by echoing the variable.

Multiple PHP If Statements

You can combine multiple if checks into one statement by using the "&" operator, and as such, you do not need to write a statement for each and every check you will be doing.

<?php
$Number = 5;
$Letter = 'T';

if (($Number == '5')
 && ($Letter == 'T')) { // PHP If Statement with multiple Checks
  echo 'Both the Number and the Letter matched!';
} else { // PHP Else
  echo 'Something did not match!';
}

?>

Adding a third check to the same statement, would simply be done like below:

<?php
$Number = 5;
$Letter = 'T';
$Word = 'Brugbart';

if (($Number == '5')
 && ($Letter == 'T')
 && ($Word == 'Brugbart')) { // PHP If Statement with multiple Checks
  echo 'Both the Number, Letter and Word matched!';
} else { // PHP Else
  echo 'Something did not match!';
}

?>

Abbreviated If statements

Abbreviated If Statements is a shortened form of these if statements which is leaving out the Curly Brackets.

These statements are useful when you only have a single line of code to be executed, allowing you can leave out the Curly Brackets for your convenience. It can however quickly course problems if you forget it later, and then change the statement. Below is an example:

if ($RandomNumber == '1')
  echo 'The number was 1';
  echo 'More stuff..'; // will be executed regardless of the check.

Another Example, including an else.

if ($RandomNumber == '1')
  echo 'The number was 1';
else
  echo 'The number was not 1';

Using colon instead of Curly Brackets

You can also use colon instead of curly brackets, this is done like below.

if ($RandomNumber == '1'):
  echo 'The number was 1';
elseif ($RandomNumber == '2'):
  echo 'The number was 2';
elseif ($RandomNumber == '3'):
  echo 'The number was 3';
else:
  echo 'The number was 4';
endif;

Operators

Its also useful to know about the different operators, which allows you to do various things in your statements. These can be used to make comparisons like, less then, greater then, and not equal to, etc. The below table is showing the various operators used.

< Less then
> Greater then
= Equal to
== Exactly Equal to
!= Not Equal to
<= Less then or Equal to
>= Greater then or Equal to

Note. These operators are not exclusive to if statements.

The next Tutorial is about PHP Switch Statements