Programming Basics
  Home arrow Programming Basics arrow Page 4 - Operators, Conditionals, and PHP Decis...
Codewalker Forums 
  Tutorials  
Database Articles  
Miscellaneous  
Navigation Usability  
PEAR Articles  
Programming Basics  
Server Administration  
XML Tutorials  
  Reviews  
Database Book Reviews  
Linux Book Reviews  
Miscellaneous Reviews  
PHP Book Reviews  
PHP Software Reviews  
Server Admin Reviews  
SQL Tool Reviews  
  Code Gallery  
Content Management Code  
Contest Code  
Counters Code  
Database Code  
Date Time Code  
Discussion Board Code  
Email Code  
File Manipulation Code  
GUI Code  
Link Farm Code  
Miscellaneous Code  
Search Code  
Site Navigation Code  
User Management Code  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Download TestComplete 
Forums Sitemap 
Weekly Newsletter 
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PROGRAMMING BASICS

Operators, Conditionals, and PHP Decision-Making
By: O'Reilly Media
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 3
    2007-10-11

    Table of Contents:
  • Operators, Conditionals, and PHP Decision-Making
  • Logical operators
  • Conditionals
  • The else statement

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Operators, Conditionals, and PHP Decision-Making - The else statement


    (Page 4 of 4 )

    The optional else statement (see Example 4-6) provides a default block of code that executes if the condition returned is FALSE. else cannot be used without an if statement, as it doesn't take a conditional itself. So, else and if have to always be together in your code.

    Example 4-6. else and if statements

    if ($username == "Admin"){
        
    echo ('Welcome to the admin page.');
    }
    else {
        
    echo ('Welcome to the user page.');
    }

    Remember to close out the code block from the if conditional when you've used braces to start your block of code. Similar to the if block, the else block should also use curly braces to begin and end the code.

    The elseif statement

    All of this is great except for when you want to test for several conditions simultaneously. To do this, you can use the elseif statement. It allows for testing of additional conditions until one is found to be true or until you hit the else block. Each elseif has its own code block that comes directly after the elseif condition. The elseif must come after the if statement and before an else statement if one exists.

    The elseif structure is a little complicated, but Example 4-7 should help you understand it.

    Example 4-7. Checking multiple conditions

    if ($username == "Admin"){
       
    echo ('Welcome to the admin page.');
    }
    elseif ($username == "Guest"){
       
    echo ('Please take a look around.');
    }
    else {
       
    echo ("Welcome back, $username.");
    }

    Here you can check for two conditions and take different actions based on each of the values for $username. Then you also have the option to do something else if the $username isn't one of the first two.

    The next construct builds on the concepts of the if/ else statement, but it allows you to efficiently check the results of an expression to many values without having a separate if/else for each value.

    Please check back next week for the conclusion to this article.


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

       · This article is an excerpt from the book "Learning PHP and MySQL, Second Edition,"...
     

    Buy this book now. This article is excerpted from chapter four of the book Learning PHP and MySQL, Second Edition, written by Michele Davis and Jon Phillips (O'Reilly, 2006; ISBN: 0596101104). Check it out today at your favorite bookstore. Buy this book now.

    PROGRAMMING BASICS ARTICLES

    - PHP: Hypertext Preprocessor: What is it?
    - Loops and PHP Decision Making
    - Operators, Conditionals, and PHP Decision-Ma...
    - PHP Decision-Making
    - Coding
    - Server Statistics
    - Looping in PHP
    - Cookies in PHP
    - Working with text files
    - Beginning Object Oriented Programming in PHP
    - A Tour of Decision Making Structures in PHP
    - PHP Strings Primer
    - PHP Control Structures
    - Intro to Vim
    - Reading Directorys with PHP





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 1 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek