PHP
  Home arrow PHP arrow Page 7 - PHP 101 (part 5) - The Wonderland Fact...
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 5) - The Wonderland Factor
By: Vikram Vaswani and Harish Kamath, (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 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:
      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 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!
    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

     

       

    PHP ARTICLES

    - Validating Web Forms with the Code Igniter P...
    - Output Buffering
    - Paginating Database Records with the Code Ig...
    - HTTP Headers in Web Development
    - Project Management: Administration
    - 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





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