Things To Know About If Statements In PHP

Going back to basics can be useful, even for the most experienced developers. Different languages act and respond in different ways and knowing exactly how some control structures work can be very useful in the long run.

I remember when coding in VB (some years ago now) and realizing that an if statement didn't work like I had expected it to work. I had expected that as soon as a condition was met the code would execute whatever was in that condition and drop out of the if statement. What was actually happening was that the code for any condition met was being executed. I'm not sure if VB still works like that, but it made me realize that a developer must know these things or they could write a system crippling bug without even realizing it.

PHP if statements have been written with efficiency in mind. If a condition is met then the code in the condition is run and the rest of the if statement is skipped. Take the following simple example:

$value = 1;
 
if ( $value == 1 ) {
  echo 'Value is 1';
} elseif ( $value == 2 ) {
  echo 'Value is 2';
} elseif ( $value == 3 ) {
  echo 'Value is 3';
} elseif ( $value == 4 ) {
  echo 'Value is 4';
} elseif ( $value == 5 ) {
  echo 'Value is 5';
} elseif ( $value == 1 ) {
  echo 'Value is another 1';
}

The string "Value is 1" is printed to the browser, the last condition is never met and so is not run.

This example can be taken further by looking at how boolean operations effect the outcome of if statements. Take the following example if statement with an or condition.

$value = 1;
 
if ( $value == 1 || wibble($value == 2) ) {
  echo 'Value is 1';
} elseif ( $value == 1 ) {
  echo 'Value is another 1';
}

You might think that this would cause an error when PHP tries to run the non-existent function called wibble(). However, because the first part of the OR command is true, PHP never tries to run the second half. This is important to know if you are running a function that does something and comes back with a success or failure. It would never get run in the first place if the first half is true. Changing this to an AND command would cause the code to fail as it will try to evaluate both parts of the statement, or does it? Take a look at the following code, where I have changed the $value to be 3.

$value = 3;
 
if ( $value == 1 && wibble($value == 2) ) {
  echo 'Value is 1';
} else {
  echo 'Fail';
}

This code will not error because the first part of the AND is false, so PHP skips the second part because there is no point in evaluating the second half as well. Take a look at the following code:

if ( FALSE && $value == 1 ) {
}

This if statement would never try to evaluate the value of $value as the first part of the statement is false. This effect does have it's uses, for example, the following statement would never fall over with a file not found error due to the fact that the file_exists() function would produce a false result and therefore never run the second command.

if ( file_exists($filename) && filemtime($filename) > time() ) {
  do_something_with_file();
}

Add new comment

The content of this field is kept private and will not be shown publicly.
CAPTCHA
19 + 1 =
Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.