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 larger than other value)
do stuff
else
do something elseDifferent methods
To my knowledge, there are a few different ways to do this.
- The normal way using shorthand
- The normal way with curly brackets
- The normal way with colon
- 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";
else
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 { } for clarity. I like this.
Colon method
This is clearer when you have a lot of HTML between the if and 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
of course, there are infinitely many possible outcomes, not only three. I 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.