Tutorial 25:: Simple Arguments
Tutorial 25:: Simple Arguments
Functions can be incredibly powerful, because you can program them to do almost
anything you want them to. One important step in making your functions even smarter
is to use "arguments" within your function. Not the type of argument where you and a
friend are bickering about a disagreement, I'm talking about arguments within
functions — two very different things.
Think of an argument like a variable. Your program can pass extra information to your
functions using arguments. You specify your arguments within the parenthesis after
your function name, and you can have as many as you want, as long as they're
comma separated.
1 <?php ?
2
3
function
hangTen($location)
{
4
5
echo
"We're
surfing
in
$location!<br>";
6
7
}
8
9
hangTen("Hawaii");
10
hangTen("California");
11
hangTen("Newfoundland");
12
13 ?>
So, in the above example, we're passing an argument to the hangTen() function. Later
in our script, we call our function several times with a string of text within each
parenthesis, and each time we call our function with a new argument value, that value
will display on the screen along with the text we provided in our function.
1 <?php ?
2
3
function
multiplyTogether($val1,
$val2)
{
4
5
$product
=
$val1
*
$val2;
6
echo
"The
product
of
the
two
numbers
is:
$product";
7
8
}
9
10
multiplyTogether(14,
27);
11
12 ?>
All we did here was add another argument, separated by comma, then when we called
our multiplyTogether() function later in the script, we provided two values to take
the place of our $val1 and $val2 arguments.
The result?
The
product
of
the
two
numbers
is:
378
Feel free to experiment with Custom Functions using the practice.php page provided
in this folder.
Check
out
the
final
example
Previous
Lecture Next
Lecture