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!