Home arrow PHP arrow Page 7 - PHP 101 (part 5) - The Wonderland Factor

The Wonderland Factor - PHP

This concluding article in the series illustrates PHP's filefunctions, with examples of how to read and write files on the system. Italso includes an explanation of user-defined functions, return values andfunction arguments, together with some not-so-real-life examples.

TABLE OF CONTENTS:
  1. PHP 101 (part 5) - The Wonderland Factor
  2. So Who Are You, Anyway?
  3. Graffiti For The Masses
  4. Calling Godzilla
  5. Q
  6. Arguments And Responses
  7. The Wonderland Factor
By: Vikram Vaswani and Harish Kamath, (c) Melonfire
Rating: starstarstarstarstar / 7
September 06, 2000

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement
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
 

blog comments powered by Disqus
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates

 



© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap

Dev Shed Tutorial Topics: