Things To Know About If Statements In PHP

9th February 2009 - 4 minutes read time

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:

  1. $value = 1;
  3. if ( $value == 1 ) {
  4. echo 'Value is 1';
  5. } elseif ( $value == 2 ) {
  6. echo 'Value is 2';
  7. } elseif ( $value == 3 ) {
  8. echo 'Value is 3';
  9. } elseif ( $value == 4 ) {
  10. echo 'Value is 4';
  11. } elseif ( $value == 5 ) {
  12. echo 'Value is 5';
  13. } elseif ( $value == 1 ) {
  14. echo 'Value is another 1';
  15. }

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.

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

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.

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

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:

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

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.

  1. if ( file_exists($filename) && filemtime($filename) > time() ) {
  2. do_something_with_file();
  3. }

Add new comment

The content of this field is kept private and will not be shown publicly.