Programming Basics
  Home arrow Programming Basics arrow Page 2 - Loops and PHP Decision Making
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

Loops and PHP Decision Making
By: O'Reilly Media
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 5
    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


    Loops and PHP Decision Making - Breaking out


    (Page 2 of 5 )

    If you want only the code in the matching block to execute, place a break keyword at the end of that block. When PHP comes across the break keyword, processing jumps to the next line after the entire switch statement. Example 4-11 illustrates how
    processing works with no break statements.

    Example 4-11. What happens when there are no break keywords

    $action = "ASSEMBLE ORDER";
    switch ($action) {
        case "ASSEMBLE ORDER":
            echo "Perform actions for order assembly.<br />";
        case "PACKAGE":
            echo "Perform actions for packing.<br />";
        case "SHIP":
            echo "Perform actions for shipping.<br />";
        }

    If the value of $action is "ASSEMBLE ORDER", the result is:

      Perform actions for order assembly.
      Perform actions for packing.
      Perform actions for shipping.

    However, if a user has already assembled an order, a value of "PACKAGE" produces the following:

      Perform actions for packing.
     
    Perform actions for shipping.

    Defaulting

    The SWITCH statement also provides a way to do something if none of the other cases matches, which is similar to the else statement in an if, elseif, or else block.

    Use the DEFAULT: statement for the SWITCH's last case statement (see Example 4-12).

    Example 4-12. Using the DEFAULT: statement to generate an error

    switch ($action) { 
        case "ADD":
            echo "Perform actions for adding.";
            break;
       
    case "MODIFY":
            echo "Perform actions for modifying.";
           
    break;
       
    case "DELETE":
           
    echo "Perform actions for deleting.";
           
    break;
       
    default:
            echo "Error: Action must be either ADD, MODIFY, or DELETE.";
    }

    The switch statement also supports the alternate syntax in which the switch and endswitch keywords define the start and end of the switch instead of the curly braces {}, as shown in Example 4-13.

    Example 4-13. Using endswitch to end the switch definition

    switch ($action):
       
    case "ADD":
           
    echo "Perform actions for adding.";
           
    break;
       
    case "MODIFY":
           
    echo "Perform actions for modifying.";
           
    break;
       
    case "DELETE":
           
    echo "Perform actions for deleting.";
           
    break;
       
    default:
            echo "Error: Action must be either ADD, MODIFY, or DELETE.";
    endswitch;

    You've learned that you can have your programs execute different code based on conditions called expressions. The switch statement provides a convenient format for checking the value of an expression against numerous possible values.

    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

    - 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 4 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek