A good[ish] website

Web development blog, loads of UI and JavaScript topics

PHP if, else and elseif statements

Filed under: WordPress/PHP— Tagged with: conditional

Here's all the different ways to do conditional logic in PHP.

The bare-bones basic syntax in plain english goes something like this:

if (value is lagrer than other value)
  do stuff
  do something else

Different methods

To my knowledge there are few different ways to do this.

  1. The normal way using shorthand
  2. The normal way with curly brackets
  3. The normal way with colon
  4. The ternary method

Actually only two, the first three are exactly the same. Lets see.

The shorthand method

if ($a > $b)
  echo "a is larger than b";
  echo "a is smaller than b";

But, as they say "Explicit is always better than implicit". More on that here.

The curly bracket method, the more explicit method

if ($a > $b) {
  echo "a is larger than b";
} else {
  echo "a is smaller than b";

It's good to use { } to be more clear. I like this.

Colon method

This is more clear when you have a lot of html in between the if else statements.

<?php if ($a > $b) : ?>
  <!--Loads of html content here-->
<?php else : ?>
  <!--Loads of html content here-->
<?php enfif; ?>

The ternary method

This one is also referred as the shorthand method. It is probably the most confusing and it has its limitations compared to the normal statement, but also more handy in many cases.

$age_string = ($age < 18) ? 'Cannot buy booze' : 'Can buy booze';

That is equal to:

if ($age < 18) {
  $age_string = 'Cannot buy booze';
} else {
  $age_string = 'Can buy booze';

Much more compact!

The limitation in it being that you can't echo out stuff in the statement. The following will not work.

($age < 18) ? echo 'Cannot buy booze' : echo 'Can buy booze';

The way you would make the echo work with it:

$age_string = ($age < 18) ? 'Cannot buy booze' : 'Can buy booze';
echo $age_string;

But there is more...

...than two "states" the if and the else. A value can have three "states" so to speak: smaller, larger and equal to. That's when the elseif steps in, (of course there is ∞ amount of different outcomes, not only three, hope you get the point).

The upper mentioned statements all have a flaw. See comments below.

if ($a > $b) {
  echo "a is larger than b";
} else {
  // This is also true when the two values are equal, so it's not smaller
  echo "a is smaller than b";

The following is straight from the PHP Manual elseif/if else article.

if ($a > $b) {
  echo $a . " is greater than " . $b;
} elseif ($a == $b) { // Note the combination of the words.
  echo $a . " equals " . $b;
} else {
  echo $a." is neither greater than or equal to ".$b;

Here’s the same with comments.

if ($a > $b) {
  echo $a . " is greater than " . $b; //First state:  if "a" is larger
} elseif ($a == $b) {
  echo $a." equals ".$b;              //Second state: if "a" and "b" are equal
} else {
  echo $a . " is smaller " . $b;      //Third state:  if "a" smaller

The elseif does not mean that something "is equal to", it's just a way to test it among millions of other things.

I quote the manual:

elseif, as its name suggests, is a combination of if and else. Like else, it extends an if statement to execute a different statement in case the original if expression evaluates to FALSE. However, unlike else, it will execute that alternative expression only if the elseif conditional expression evaluates to TRUE.

else and if combined... To me it always kind of said or. Does that make sense?

Hope this was helpful.

Comments would go here, but the commenting system isn’t ready yet, sorry.

  • © 2022 Antti Hiljá
  • About
  • All rights reserved yadda yadda.
  • I can put just about anything here, no one reads the footer anyways.
  • I love u!