PHP - function
Calling a function in PHP
Note that echo is not a
function.[1] "Calling a function"
means causing a particular
function to run at a particular
point in the script. The basic
ways to call a function include:
calling the function to write on a
new line (such as after a ";" or
"}")
|
print('I am Naresh, I am.'); |
calling the function to write on a
new line inside a control
<?php
if ($a=72){
print('I am Naresh, I am.');
}
?>
|
xcalling a function inside the
argument parentheses (expression)
of a control
<?php
while ($i < count($one)){
}
?> |
In our earlier examples we have
called several functions. Most
commonly we have called the
function print() to print text to
the output. The parameter for echo
has been the string we wanted
printed (for example print("Hello
World!") prints "Hello World!" to
the output).
If the function returns some
information we assign it to a
variable with a simple =:
Parameters
Parameters are variables that
exist only within that function.
They are provided by the
programmer when the function is
called and the function can read
and change them locally (except
for reference type variables,
which are changed globally - this
is a more advanced topic).
When declaring or calling a
function that has more than one
parameter, you need to separate
between different parameters with
a comma ','.
A function declaration can look
like this:
function print_two_strings($var1,
$var2)
{
echo $var1;
echo "\n";
echo $var2;
return NULL;
} |
To call this function you must
give the parameters a value. It
doesn't matter what the value it,
as long as there is one.
function call:
|
print_two_strings("hello",
"world"); |
Output
When declaring a function you
sometimes want to have the freedom
not to use all the parameters,
therefore PHP allows you to give
them default values when declaring
the function:
function print_two_strings($var1
= "Hello World", $var2 = "I'm
Learning PHP")
{
echo($var1);
echo("\n");
echo($var2);
} |
These values will only be used
if the function call does not
include enough parameters. If
there is only one parameter
provided then $var2 = "I'm
Learning PHP".
function call:
|
print_two_strings("hello"); |
Output :-
Another way to have a dynamic
number of parameters is to use
PHP's built-in func_num_args,
func_get_args, and func_get_arg
functions.
function mean()
{
$sum = 0;
$param_count = func_num_args();
for ($i = 0; $i < $param_count;
$i++)
{
$sum += func_get_arg($i);
}
$mean = $sum / $param_count;
echo "Mean: {$mean}";
return NULL;
}
|
Or
function mean()
{
$sum = 0;
$vars = func_get_args();
for ($i = 0; $i < count($vars);
$i++)
{
$sum += $vars[$i];
}
$mean = $sum / count($vars);
echo "Mean: {$mean}";
return NULL;
}
|
The above functions would
calculate the arithmetic mean of
all of the values passed to them
and output it. The difference is
that the first function uses
func_num_args and func_get_arg,
while the second uses
func_get_args to load the
parameters into an array. The
output for both of them would be
the same. For example:
Output
Returning a value
This function is all well and
good, but usually you will want
your function to return some
information. Generally there are 2
reasons why a programmer would
want information from a function:
- The function does tasks such
as calculations, and we need the
result.
- A function can return a
value to indicate if the
function encountered any errors.
To return a value from a
function use the return()
statement in the function.
function add_numbers($var1 =
0, $var2 = 0, $var3 = 0)
{
$var4 = $var1 + $var2 + $var3;
return $var4;
} |
Example PHP script:
<?php
function add_numbers($var1 =
0, $var2 = 0, $var3 = 0)
{
$var4 = $var1 + $var2 + $var3;
return $var4;
} $sum
= add_numbers(1,6,9);
echo "The result of 1 + 6 + 9
is {$sum}";
?>
|
Result:
The result of 1 + 6 + 9 is 16
|
Notice that a return() statement
ends the function's course. If
anything appears in a function
declaration after the return()
statement is executed, it is
parsed but not executed. This can
come in handy in some cases. For
example:
<?php
function divide ($dividee,
$divider) {
if ($divider == 0) {
//Can't divide by 0.
return false;
}
$result = $dividee/$divider;
return $result;
}
?>
|
Notice that there is no else after
the if. This is due to the fact
that if $divider does equal 0, the
return() statement is executed and
the function stops.
If you want to return multiple
variables you need to return an
array rather than a single
variable. For example:
<?php
function maths ($input1,
$input2) {
$total = ($input1 + $input2);
$difference = ($input1 -
$input2);
$ret = array("tot"=>$total,
"diff"=>$difference);
return $ret;
}
?>
|
When calling this from your
script you need to call it into an
array. For example:
<?php
$return=maths(10, 5);
?>
|
In this case $return['tot']
will be the total (eg 15), while $return['diff']
will be the difference (5).
|