PHP
  Home arrow PHP arrow Page 7 - PHP 101 (Part 2) - Shakespeare's Rose
Dev Shed Forums 
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Moblin 
JMSL Numerical Library 
VPS Hosting 
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? 
PHP

PHP 101 (Part 2) - Shakespeare's Rose
By: Vikram Vaswani and Harish Kamath, (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 10
    2000-08-08

    Table of Contents:
  • PHP 101 (Part 2) - Shakespeare's Rose
  • Form...
  • ...And Function
  • Operating With Extreme Caution
  • Shakespeare In The Matrix
  • If Not This, Then What?
  • Fortune Smiles
  • Submitting To The King
  • Miscellaneous Notes

  • 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


    PHP 101 (Part 2) - Shakespeare's Rose - Fortune Smiles


    (Page 7 of 9 )

    PHP also provides you with a way of handling multiple possibilities - the "if-elseif-else" construct. A typical "if-elseif-else" statement block would look like this:

    if (first condition is true) { do this! } elseif (second condition is true) { do this! } elseif (third condition is true) { do this! } ... and so on ... else { do this! }
    And here's an example that demonstrates how to use it:


    <html> <head> <style type="text/css"> td {font-family: Arial;} </style> </head> <body> <font face="Arial" size="+2"> The Amazing Fortune Cookie Generator </font> <form method="GET" action="cookie.php4"> <table cellspacing="5" cellpadding="5" border="0"> <tr> <td align="center"> Pick a day </td> <td align="right"> <select name="day"> <option value="Monday">Monday <option value="Tuesday">Tuesday <option value="Wednesday">Wednesday <option value="Thursday">Thursday <option value="Friday">Friday <option value="Saturday">Saturday <option value="Sunday">Sunday </select> </td> </tr> <tr> <tr> <td colspan="2" align="center"> <input type="submit" value="Hit me!"> </td> </tr> </table> </form> </body> </html>
    As you can see, this is simply a form which allows you to pick a day of the week. The real work is done by the PHP script "cookie.php4"


    <? if ($day == "Monday") { $fortune = "Never make anything simple and efficient when a way can be found to make it complex and wonderful."; } elseif ($day == "Tuesday") { $fortune = "Life is a game of bridge -- and you've just been finessed."; } elseif ($day == "Wednesday") { $fortune = "What sane person could live in this world and not be crazy?"; } elseif ($day == "Thursday") { $fortune = "Don't get mad, get interest."; } elseif ($day == "Friday") { $fortune = "Just go with the flow control, roll with the crunches, and, when you get a prompt, type like hell."; } else { $fortune = "Sorry, closed on the weekend"; } ?> <html> <head> <basefont face="Arial"> </head> <body> Here is your fortune for <? echo $day; ?>: <br> <b><? echo $fortune; ?></b> </body> </html>
    In this case, we've used the "if-elseif-else" control structure to assign a different fortune to each day.

    There's one important point to be noted here - as soon as one of the "if" statements within the block is found to be true, PHP will execute the corresponding code, skip the remaining "if" statements in the block, and jump immediately to the lines following the entire "if-elseif-else" block.{mospagebreak title=A Little Bit Of Logic} Now, you've already seen that PHP allows you to nest conditional statements. However, if you take another look at the example we used to demonstrate the concept


    <? if ($day == "Thursday") { if ($time == "12") { if ($place == "Italy") { $lunch = "pasta"; } } } ?>
    you'll agree that is both complex and frightening. And so, in addition to the comparison operators we've used so liberally thus far, PHP also provides a few logical operators which allow you to group conditional expressions together. The following table should make this clearer.

    Operator

    What It Means

    Example

    Evaluates To

    &&

    AND

    $delta == $gamma && $delta > $omega

    True

    $delta && $omega < $omega

    False

    ||

    OR

    $delta == $gamma || $delta < $omega

    True

    $delta > $gamma || $delta < $omega

    False

    !

    NOT

    !$delta

    False

    <=

    is less than or equal to

    $delta <= $omega

    False



    Given this knowledge, it's a simple matter to rewrite the example above in terms of logical operators:

    <? if ($day == "Thursday" && $time == "12" && $place == "Italy") { $lunch = "pasta"; }
    Simple and elegant? Yes.{mospagebreak title=Switching Things Around} An alternative to the "if-else" family of control structures is PHP's "switch" statement, which does almost the same thing. It looks like this


    switch (decision-variable) { case first_condition_is true: do this! case second_condition_is true: do this! case third_condition_is true: do this! ... and so on... }
    We'll make this a little clearer by re-writing our fortune cookie example in terms of the "switch" statement.

    [cookie.php4]

    <? // the decision variable here is the day chosen by the user switch ($day) { // first case case "Monday": $fortune = "Never make anything simple and efficient when a way can be found to make it complex and wonderful."; break; // second case case "Tuesday": $fortune = "Life is a game of bridge -- and you've just been finessed."; break; case "Wednesday": $fortune = "What sane person could live in this world and not be crazy?"; break; case "Thursday": $fortune = "Don't get mad, get interest."; break; case "Friday": $fortune = "Just go with the flow control, roll with the crunches, and, when you get a prompt, type like hell."; break; // if none of them match... default: $fortune = "Sorry, closed on the weekend"; break; } ?> <html> <head> <basefont face="Arial"> </head> <body> Here is your fortune for <? echo $day; ?>: <br> <b><? echo $fortune; ?></b> </body> </html>
    There are a couple of important keywords here: the "break" keyword is used to break out of the "switch" statement block and move immediately to the lines following it, while the "default" keyword is used to execute a default set of statements when the variable passed to "switch" does not satisfy any of the conditions listed within the block.

    More PHP Articles
    More By Vikram Vaswani and Harish Kamath, (c) Melonfire


     

       

    PHP ARTICLES

    - Building a Database-Driven Application with ...
    - User Authentication for a Project Management...
    - Introduction to the CodeIgniter PHP Framework
    - Adding Users for a Project Management Applic...
    - Migrating Class Code for a MIME Email to PHP...
    - Login and Logout Authentication for a Projec...
    - Composing Messages in HTML for MIME Email wi...
    - Project Management: Authentication
    - A Better Way to Determine MIME Types for MIM...
    - Project Management Overview
    - Handling Attachments in MIME Email with PHP
    - Completing the Project Management Application
    - Sending MIME Email with PHP
    - Handling Files for a Project Management Appl...
    - Viewing and Editing Tasks for a Project Mana...





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