Home Logo
Show Full Menu
Hide Full Menu

Elseif Statments

Created: 01/10/10 Views: 1173 myFave Image

In this tutorial we will learn Elseif Statements
We have already covered what an If Else Statement is in the previous tutorial (if you have not read this, then do so before you carry on with this tutorial) and the Elseif statement is just another add-on. With Elseif, we can check numerous conditions before we set an else results if all the conditions are false, however if one of the conditions are true, then it will just act like a normal if statement.

To write an If Else Statement is simple
  • if( //First Condition to check ) {
  • //Requested task if condition is true
  • }elseif( //Second Condition to check ){
  • //Requested task if condition is true
  • }else{
  • //Requested task if all condition are false
  • }

An Elseif Statement can only be used after the an If Statement, otherwise the statement will result in errors.

Here is a simple example of an If Else Statement
  • $name = "John";
  • if($name == "Frank"){
  • print "Hey Frank!";
  • }elseif($name == "John"){
  • print "Hey John!";
  • }else{
  • print "Hey Guest!";
  • }

  • Hey John!
So from this example, the statement is checking if the variable 'name' is equal to 'Frank', if it is, print "Hey Frank!", if it isn't check to see if variable 'name' is equal to John, if it is, print "Hey John!", if it's not print "Hey Guest!".

Copy and try for yourself!
  • $car = "BMW";
  • if($car == "VW"){
  • print "You've chosen VW";
  • }elseif($car == "Mercedes"){
  • print "You've chosen Mercedes";
  • }elseif($car == "Ford"){
  • print "You've chosen Ford";
  • }elseif($car == "BMW"){
  • print "You've chosen BMW";
  • }elseif($car == "Audi"){
  • print "You've chosen Audi";
  • }else{
  • print "Your choice is not available";
  • }
  • You've chosen BMW

  • $theNum = 11;
  • if($theNum > 11){
  • print "Larger";
  • }elseif($theNum < 11){
  • print "Smaller";
  • }

  • //No Result

This is only a few basic examples, but it should be enough for you to understand the basic method of writing an Elseif Statement and why you would use them in your PHP scripting.

next tutorial: Arrays

There are no comments