Else If 
Else If
by Brad Hussey
Video Lecture 18 of 45
Not yet rated
Views: 771
Date Added: May 14, 2017

Lecture Description

Lecture 18: Else if

There's one last piece in the IF / ELSE puzzle. It's called ELSEIF (pronounced "Else If"). ELSEIF is a kind of combination of IF and ELSE. PHP.net puts it nicely:

"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."

If the above explanation is as clear as mud, the syntax looks like this:

if (expression) {

// code to execute if the above expression is TRUE

} elseif (different expression) {

/* code to execute if first expression is FALSE

but the ELSEIF expression is TRUE */

} else {

/* code to execute if neither

of the above expressions are TRUE */

}

Now, if we added some real PHP, it would look like this:


$native_language = "Spanish";

if ($native_language == "French") {

echo "Bonjour! Vouz parlez Français.";

} elseif ($native_language == "Spanish") {

echo "¡Hola! Usted habla Español.";

} else {

echo "Hello! You probably speak English.";

}


Here's the commented code:


// Setting the variable

$native_language = "Spanish";

// IF native language is French

if ($native_language == "French") {

// Echo some french!

echo "Bonjour! Vouz parlez Français.";

// ELSEIF native language is Spanish

} elseif ($native_language == "Spanish") {

// Echo some spanish!

echo "¡Hola! Usted habla Español.";

// ELSE native language is neither of the above

} else {

// Echo some english!

echo "Hello! You probably speak English.";

}

DOWNLOAD COURSE FILES HERE
www.bradhussey.ca/download-php-course

Course Index

Course Description

This course is a Total Beginner's Guide to Coding Your Very Own Dynamic Websites with PHP, so you need no prior knowledge or experience — although it's a good idea that you know some HTML (my beginner's guide "Build a Website from Scratch with HTML & CSS" will get teach you everything you need to know).

So — why learn PHP? Well, PHP is a very powerful scripting language used by millions of websites. Some of the most popular websites and frameworks utilize PHP to build their dynamic websites. PHP works very well with HTML, and therefore will allow you to start coding dynamic websites quickly without having to learn some of the more complicated scripting languages out there.

Comments

There are no comments. Be the first to post one.
  Post comment as a guest user.
Click to login or register:
Your name:
Your email:
(will not appear)
Your comment:
(max. 1000 characters)
Are you human? (Sorry)