Get DevWP - WordPress Development Theme

PHP if, elseif, else – Conditional Statements

Decision Making in PHP

One of the best parts of Dynamic Programming Languages like PHP and others, is the ability to make decisions based on various scenarios.

Let’s say you have a person visiting your website and the content they’ll see will be based on their age. You can have some basic content for the general public that’s age appropriate for all and when the person logs into the site, the content can be adjusted to provide them with an experience best suited for their age group.

Basically you have a system in place for Kids, Teenagers, Adults and Older Adults. So you have four different groups of people your catering too (really 5 groups when considering people not logged in). You can use the if, elseif, else conditional statements to help you along the way.

Below is a very basic breakdown of how the code could look like.

Note: in some examples, I’ll remain in PHP Mode and in other examples, I’ll jump in and out of PHP Mode.

Note: you will often see elseif and else if. Both work when curly braces are used but elseif is what’s used with the alternative syntax as demonstrated further below. I stick with elseif in my code.



<?php
$age = 31;

if ( $age < 13 ) {
	echo 'Here is your Kid Friendly Content';
} elseif ( $age > 12 && $age < 20 ) {
	echo 'Here is your Teenage Friendly Content';
} elseif ( $age > 19 && $age < 65 ) {
	echo 'Here is your Adult Friendly Content';
} else {
	echo 'Here is your Older Adult Friendly Content';
}
?>
		

The output of the code above will be “Here is your Adult Friendly Content”.

So how is the code actually working?

  1. if age is less than 13 and if so, echo out the string of text for that age group
  2. elseif age is greater than 12 but less than 20, echo out the string of text for teenagers
  3. elseif age is greater than 19 but less than 65, echo out the text for adults
  4. else age must be equal to or greater than 65 so we echo out the Older Adult string of text

The PHP Parser will process the code and output the first condition that evaluates too true. It goes from the first condition to the next until it reaches one that is correct based on what’s supplied.

Here is some Pseudocode to look at.



if ( condition ) {
	code to be executed;
} elseif ( condition ) {
	code to be executed;
} elseif ( condition ) {
	code to be executed;
} else {
	code to be executed when all above conditions aren't true;
}


You don’t always need to have an elseif or even an else in your conditional checks. You can have a simple if conditional or an if else. Checkout the example below.



<?php

$loggedIn ='in';

if ($loggedIn == 'in') {
	echo 'Logged In';
} else {
	echo 'Not Logged In';
}
?>

Alternative if else Syntax

You will notice in the code snippets above we have an opening { and a closing }. You can also replace the opening { with : and replace the closing } with endif;



<?php

$loggedIn = 'out';

if ($loggedIn == 'out'): ?>
Please Login
<?php endif; ?>


Here is an example of the alternative syntax with the elseif part.


<?php
$a = 10;
if ( $a == 8 ):
	echo "a equals 8";
elseif ( $a == 10 ):
	echo "a equals 10";
else:
	echo "a is not equal to 8 or 10";
endif;
?>

Note: when using the alternative syntax, elseif needs to be one word. If you separate else if, then you will generate a parse error.

The Takeaway

You can really start seeing the power of PHP when you start using its conditional statements and control structures.

Copy the code and try it out. Alter it and learn from it.



View Our Themes

Let's Connect