SunQuest
 
       Programming Basics
  Home arrow Programming Basics arrow Loops and PHP Decision Making
IBM developerWorks
Try It Free
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  
Forums Sitemap 
Dedicated Servers  
Download TestComplete 
IBM® developerWorks
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

Loops and PHP Decision Making
By: O'Reilly Media
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 4
    2007-10-18

    Table of Contents:
  • Loops and PHP Decision Making
  • Breaking out
  • Looping
  • for Loops
  • Breaking Out of a Loop

  • 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

    Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here

    Loops and PHP Decision Making


    (Page 1 of 5 )

    In this third part of a three-part series on PHP, you will learn about loops and more. 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). Copyright © 2006 O'Reilly Media, Inc. All rights reserved. Used with permission from the publisher. Available from booksellers or direct from O'Reilly Media.

    The ? Operator

    The ? operator is a ternary operator, which means it takes three operands. It works like an if statement but returns a value from one of the two expressions. The conditional expression determines the value of the expression. A colon (:) is used to separate the expressions, as shown here:

      {expression} ? return_when_expression_true : return_when_expression_false;

    Example 4-8 tests a value and returns a different string based on whether it's TRUE or FALSE.

    Example 4-8. Using the ? operator to create a message

    <?php
    $logged_in = TRUE;
    $user = "Admin";
    $banner = ($logged_in==TRUE)?"Welcome back, $user!":"Please login.";
    echo "$banner";
    ?>

    Example 4-8 produces:

      Welcome back, Admin!

    This can be pretty useful for checking errors. Now, let's look at a statement that lets you check an expression against a list of possible values to pick the executable code.

    The switch Statement

    The switch statement compares an expression to numerous values. It's very common to have an expression, such as a variable, for which you'll want to execute different code for each value stored in the variable. For example, you might have a variable called $action, which may have the values add, modify, and delete. The switch statement makes it easy to define a block of code to execute in response to each of those values.

    To illustrate the difference between using the if statement and the switch statement to test a variable for several values, we'll show you the code for the if statement (in Example 4-9), and then for the switch statement (in Example 4-10).

    Example 4-9. Using if to test for multiple values

    if ($action == "ADD") {
        echo "Perform actions for adding.";
       
    echo "As many statements as you like can be in each block.";
    }
    elseif ($action == "MODIFY") {
       
    echo "Perform actions for modifying.";
    }
    elseif ($action == "DELETE") {
       
    echo "Perform actions for deleting.";
    }

    Example 4-10. Using switch to test for multiple values

    switch ($action) {
       
    case "ADD":
    |      
    echo "Perform actions for adding.";
           
    echo "As many statements as you like can be in each block.";
           
    break;
       
    case "MODIFY":
           
    echo "Perform actions for modifying.";
           
    break;
       
    case "DELETE":
           
    echo "Perform actions for deleting.";
           
    break;
      }

    The switch statement works by taking the value after the switch keyword and comparing it to the cases in the order in which they appear. If no case matches, the code isn't executed. Once a case matches, the code is executed. The code in subsequent cases also executes until the end of the switch statement or until a break keyword. This is useful for processes that have several sequential steps. If the user has already done some of the steps, he can jump into the process where he left off.

    The expression after the switch statement must evaluate to a simple type, such as a number, an integer, or a string. An array can be used only if a specific member of the array is referenced as a simple type.

    There are numerous ways to tell PHP not to execute cases besides the matching case.

    More Programming Basics Articles
    More By O'Reilly Media


       · 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

    - 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
    - An Overview of Arrays in PHP





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway