PHP If Then Else and ElseIf Statements

Posted The: 09/05/2008 At: 11:17

Last Edited: 17/12/2009 At: 14:49

If statements are used to check for facts, and make choices there after. Often checking to see if the content of a variable match any given question, the if and else if both return either TRUE or FALSE. They can also be used to check the state of a variable, such as whether it exists in memory, or if the variable is an array.

PHP If statements often also include so called else ifs, which can be used to do additional checks, if the first check returned false.

PHP If Statements

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".

Using PHP Else if

In the next example, try to include an Else If statement. Use the same approach, but this time use 4 numbers instead. So simply extend the if statement to include one or more else if statements. Still ending with a normal else statement, because i don't need to check if the number was 4, when i can predict that it will be 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

Finally you can combine checks by using the "&" operator, and as such you do not need to write an if statement for each check you will be doing.

<?php
$DB_HashedPassword = 'Test';
$HashedPassword = 'Test';
$UserName = 'BlueBoden';

if (($UserName == 'BlueBoden')
 && ($DB_HashedPassword == $HashedPassword)) { // PHP If Statement with multiple Checks
  echo 'Welcome User!';
} else { // PHP Else
  echo 'Denied!';
}

?>

The above would be useful when checking if a user was logged in.

Abbreviated If statements

Abbreviated If Statements is a shortend form of php if/else/elseif. Leaving out the Curly Brackets.

If statements without Curly Brackets, can be used if you only have one line of code to be executed, then you can leave out the Curly Brackets. 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 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 very useful to know about the different operators, these can be used to make comparisons like, less then, greater then, 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.

Comments: [0]

© Brugbart Webdesign