PHP
  Home arrow PHP arrow Page 7 - PHP 101 (part 5) - The Wonderland Factor
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 5) - The Wonderland Factor
By: Vikram Vaswani and Harish Kamath, (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 6
    2000-09-06


    Table of Contents:
  • PHP 101 (part 5) - The Wonderland Factor
  • So Who Are You, Anyway?
  • Graffiti For The Masses
  • Calling Godzilla
  • Q
  • Arguments And Responses
  • The Wonderland Factor

  • 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 5) - The Wonderland Factor - The Wonderland Factor
    ( Page 7 of 7 )

    Our original intention was to make PHP's user-defined functions clearer via a simple calculator function, one which accepted two numbers and added, subtracted, multiplied and divided them.

    How boring!

    How about, instead, if we create a calculator, one which accepts two numbers and performs mathematical operations on them? How about if we add a twist by claiming this calculator to be the one Alice would carry on her journey through Wonderland (assuming she needed one)? And how about if we substantiate that claim by adding the Wonderland factor, a random number that skews the results of your calculations so that they're never correct?


    <?php // generate the wonderland factor // the rand() function generates a random number // in the range specified $wonderland = rand(2, 100); function calculate($type, $num1, $num2) { global $wonderland; // possible values for $type are: // 1 for addition // 2 for subtraction // 3 for multiplication // 4 for division switch($type) { case 1: $operation = "addition"; $result = $num1 + $num2 + $wonderland; $answer = "$num1 plus $num2 is $result"; return $answer; break; case 2: $operation = "subtraction"; $result = $num1 - $num2 - $wonderland; $answer = "$num1 minus $num2 is $result"; return $answer; break; case 3: $operation = "multiplication"; $result = $num1 * $num2 * $wonderland; $answer = "$num1 times num2 is $result"; return $answer; break; case 4: $operation = "division"; if($num2 != 0) { $result = ($num1 / $num2) / $wonderland ; } $answer = "$num1 by $num2 is $result"; return $answer; break; default: $result = 0; } return $result; } $type = 1; $x = 9; $y = 17; $answer = calculate($type, $x, $y); echo $answer; ?> </body> </html>
    Each time you reload the page, you should get a different answer - that's the random number at work!{mospagebreak title= Coding For The Unexpected} So far, all the functions you've seen accept a fixed set of arguments. However, you might often find yourself in a situation where you're not quite sure of how many arguments you will be passing.

    In the old days of PHP3, you had no choice but to grin and bear it. However, PHP4 allows you to create functions which will accept any number of arguments, regardless of whether or not they've been defined in the function definition.

    There are two PHP functions that add this functionality: func_num_args(), which returns the number of variables passed to the function, and func_get_args(), which lets you obtain those variables in the form of an array. Our next example will make this clearer.

    <html> <head> <basefont face=Arial> </head> <body> <?php function cart() { $arguments = func_get_args(); return $arguments; } $items = cart("toothpaste", "aspirin", "dog food", "carving knife"); ?> You have selected <? echo sizeof($items); ?> items. They are: <ol> <? for ($x=0; $x<sizeof($items); $x++) { echo "<li>" . $items[$x]; } ?> </ol> </body> </html>
    And here's the output:

    You have selected 4 items. They are: 1. toothpaste 2. aspirin 3. dog food 4. carving knife
    And that's about it for this issue of PHP 101. You should now know enough to begin writing PHP code for your Web site. We'll continue to bring you tutorials on other aspects of the language - tell us what you'd like to read about! And until next time...stay healthy!

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