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  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
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? 
Google.com  
PHP

PHP 101 (Part 2) - Shakespeare's Rose
By: Vikram Vaswani and Harish Kamath, (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 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:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log 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

    - Merging a File Split for FTP Upload using PHP
    - Getting Data from Yahoo Site Explorer Inboun...
    - Method Chaining: Adding More Selecting Metho...
    - How to Split a File During an FTP Upload Usi...
    - Expanding a Custom CodeIgniter Library with ...
    - Using the Yahoo Site Explorer Inbound Links ...
    - Building a CodeIgniter Custom Library with M...
    - Building an E-mini Trading System Using PHP ...
    - Completing the MySQL Class with Method Chain...
    - Building Dynamic Queries with Chainable Meth...
    - PHP Encryption and Decryption Methods
    - Building a MySQL Abstraction Class with Meth...
    - Completing a Sample String Processor with Me...
    - Mastering WHILE Loops for PHP and MySQL
    - Method Chaining: Adding More Methods to the ...





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