0% found this document useful (0 votes)
80 views3 pages

Tutorial 25:: Simple Arguments

Simple Arguments php

Uploaded by

Santosh Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
80 views3 pages

Tutorial 25:: Simple Arguments

Simple Arguments php

Uploaded by

Santosh Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

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.

Let's look at an example of a function with a single argument:

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.

The output will look like this:


We're
surfing
in
Hawaii!
We're
surfing
in
California!
We're
surfing
in
Newfoundland!

Let's look at an example with two arguments:

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

©2014 Brad Hussey


Code
Dynamic
Websites
with
PHP
Copyright ©2014 Brad Hussey of CodeCollege.ca

You might also like