eHow launches Android app: Get the best of eHow on the go.

How To

How to Code If, ElseIf, and Else in PHP

Member
By petlover4life
User-Submitted Article
(0 Ratings)
Code for if/elseif/else functions
Code for if/elseif/else functions
Samantha Davis

If, Else If, and Else are three sides of conditional programming in PHP which are used widely in the building of dynamic websites. In other words, these three little commands tell your computer what to do.

Difficulty: Moderate
Instructions

Things You'll Need:

  • Text editor or code programmer
  • Hosting or Apache enabled server
  1. Step 1

    Rules

    1. These tags must be contained within a PHP document and within PHP tags (< ? ? >).
    2. An "if" tag can stand alone. However, if there is an "elseif" tag, there must be an "else".
    3. An "else" tag cannot have limitations.
    4. The format is as follows:
    * if(condition){ //code here }
    * elseif(condition){ //code here }
    * else{ //code here }

  2. Step 2

    Conditions are pretty easy to understand. Any of these tags are asking the question, "Is this true?". A condition is the "this" part of the question. A very simple example could be that the sun is bright. To put this into code, we write:

    1 < ?
    2 $sun = "bright";
    3 if($sun === "bright"){
    4 echo "The sun is bright today!";
    5 }
    6 else{
    7 echo "The sun is not very bright today.";
    8 }
    9 ? >

  3. Step 3

    Let's go through this line by line. Line 1 and line 9 begin and end the PHP code snippet.

    Line 2 introduces a variable, "$sun". A variable can be identified by the "$" sign before it. Variables are traditionally composed of only alphabetic characters and underscores( _ ). Variables in PHP are traditionally lowercase, as variables are case sensitive.

    Line 3 is our if statement. Translated into normal speak, it says, "If the variable "sun" matches the string, "bright", then...". The three = signs are used to designate that you want to match a string, exactly, to another one.

    Line 4 is our conditional statement. If the if statement(line three) evaluates to true, then line four gets executed. Otherwise, it's like it never existed. In this case, we are outputting that our if statement was true.

  4. Step 4

    Line 5 ends the if statement. We could just leave it at this, and not include the else statement. If $sun was not equal to "bright", then the page would display nothing.

    Lines 6, 7 and 8 are our else statement. This is what happens if the "if statement" evaluates to false. Because there are only two options in this case, true or false, else does not get a conditional statement (in parentheses on line three).

Tips & Warnings
  • Make sure your syntax is correct, and all parenthesis and {}'s are closed.
  • Keep code simple until you get used to it.
  • Use other websites and resources to help you along.
Subscribe

Post a Comment

Post a Comment

Related Ads

  • Have you done this? Click here to let us know.
I Did This
Tags
Get Free Internet Newsletters

Copyright © 1999-2009 eHow, Inc. Use of this web site constitutes acceptance of the eHow Terms of Use and Privacy Policy.   en-US Portions of this page are modifications based on work created and shared by Google and used according to terms described in the Creative Commons 3.0 Attribution License.

Demand Media
eHow_eHow Technology and Electronics