Programming Basics
  Home arrow Programming Basics arrow Page 3 - A Tour of Decision Making Structures in PHP
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 
JMSL Numerical Library 
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

A Tour of Decision Making Structures in PHP
By: bluephoenix
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 3
    2003-10-19

    Table of Contents:
  • A Tour of Decision Making Structures in PHP
  • Comparison Operators
  • IF Statements
  • SWITCH-CASE statements
  • Conclusion

  • 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


    A Tour of Decision Making Structures in PHP - IF Statements


    (Page 3 of 5 )

    Now let's examine a scenario in which a PHP script executes a section of code only if a certain condition is met using an if-statement. An if-statement is made up of the keyword if followed by the comparison within parentheses. The block of code to be executed is set apart by braces.

    <?php
    $x 
    2;

    echo 
    "Hello, World. &lt;br /&gt;";
    if (
    $x == 2
    {
      echo 
    "\$x = $x &lt;br /&gt;";
    }
    echo 
    "Goodbye.";
    ?>

    The script first assigns the value of 2 to $x and then prints a greeting message. A logical comparison is made between $x and 2, which returns true. Because this comparison evaluates true, the script will process the braced code. The script then concludes by displaying a parting message.

    If there's only one line of code to be executed under the conditional circumstance, the braces may be omitted. The code could be re-written to resemble:

    <?php
    $x 
    2;

    echo 
    "Hello, World. &lt;br /&gt;";
    if (
    $x == 2) echo "\$x = $x &lt;br /&gt;";
    echo 
    "Goodbye.";
    ?>

    However, it is considered good practice and good form by the PEAR coding standards to still use the braces.

    IF-ELSE Statements

    The if-statement only allows for a piece of code to be executed or skipped depending on the return value of a comparison. However, it can be extended with the keyword else to allow for an alternate section of code to be processed.

    <?php
    $age 
    25;

    if (
    $age &lt13)
    {
        echo 
    "You are under 13.";
    }
    else
    {
        echo 
    "You are 13 or over.";
    }
    ?>

    In this example, an if-statement checks to see if the value of $age is less than 13. Because $age was assigned the value of 25, the first section of code would be skipped and the second section would be executed, displaying the message "You are 13 or over."

    Again, when only one line of code is used in a section then braces do not need to be used... but it is considered good practice to use them anyway.

    <?php
    $age 
    25;

    if (
    $age &lt13) echo "You are under 13.";
    else echo 
    "You are 13 or over.";
    ?>

    Question-Colon Operator

    A nice, compact way to write a short if-else statement is to use the ?: operator. Because it uses symbols instead of keywords such as if and else, some may find it difficult to start using and avoid it. However with some practice it does become easy to use and can shorten the length of your code considerably.

    The previous example can be written as follows:

    <?php
    $age 
    25;

    echo (
    $age &lt13) ? "You are under 13." "You are 13 or over.";
    ?>

    The first section of code (between the question-mark and the colon) will be used if the conditional evaluates true. The second section of code (following the colon) will be used if the evaluation is false. In the code, the statement ($age < 13) is tested and returns false so "You are 13 or over." is displayed.

    Typically, the ?: is seen when used to assign a value to a variable.

    <?php
    $a 
    100;
    $numbtype = ($a == 0) ? "even" "odd";
    echo 
    "The number $a is $numbtype.&lt;br /&gt;";

    // is equivalent to writing...

    $a 100;
    if (
    $a == 0)
    {
      
    $numbtype "even";
    }
    else
    {
      
    $numbtype "odd";
    }
      echo 
    "The number $a is $numbtype.&lt;br /&gt;";
    ?>

    IF-ELSEIF Statements

    Several conditional statements can be chained together using if-elseif statements.

    <?php
    $weather 
    "rain";
    echo 
    "The weather forcast calls for $weather. ";
    if (
    $weather == "snow")
    {
      echo 
    "You'd better bundle up! &lt;br /&gt;";
    }
    elseif (
    $weather == "rain")
    {
      echo 
    "Be sure to bring an umbrella! &lt;br /&gt;";
    }
    elseif (
    $weather == "wind")
    {
      echo 
    "Take the day off and fly a kite!&lt;br /&gt;";
    }
      else
    {
      echo 
    "Who knows what to expect?&lt;br /&gt;";
    }
    ?>

    In the example, PHP will process the chain of if-elseif. Once the first true comparison has been found, PHP will execute the desired code block and then break out of the structure. No further conditions of the structure are tested.

    More Programming Basics Articles
    More By bluephoenix


       · Wow, This is so cheap even for newbies,now i gat ma grip on dem logical...
     

    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 2 hosted by Hostway