Working with Functions
Working with Functions
numberedHeading("Widgets");
echo "<p>We build a fine range of widgets</p>";
numberedHeading("Doodads");
echo "<p>Finest in the world</p>";
?>
</body>
</html>
Saving State Between Function Calls with the static
Statement
<body>
<?php
function addFive($num) {
$num += 5;
}
$orignum = 10;
addFive($orignum);
echo $orignum;
?>
</body>
</html>
Passing Variable References to Functions
• The addFive() function accepts a single numeric
value and adds 5 to it. It returns nothing. We
assign a value to a variable $orignum and then
pass this variable to addFive().
• A copy of the contents of $orignum is stored in
the variable $num. Although we increment $num
by 5, this has no effect on the value of $orignum.
When we print $orignum, we find that its value is
still 10.
• By default, variables passed to functions are
passed by value. In other words, local copies of
the values of the variables are made.
Passing Variable References to Functions
• We can change this behavior by creating a
reference to our original variable.
• When you pass an argument to a function by
reference the contents of the variable you pass
($orignum) are accessed by the argument variable
and manipulated within the function, rather than
just a copy of the variable’s value (10).
• Any changes made to an argument in these cases
will change the value of the original variable. You
can pass an argument by reference by adding an
ampersand to the argument name in the function
definition.
Passing Variable References to Functions
}
$orignum = 10;
addFive($orignum);
echo $orignum;
?>
</body>
</html>
Testing for the Existence of a Function
• We do not always know that a function exists
before we try to invoke it. Different builds of
the PHP engine may include different
functionality, and if you are writing a script that
may be run on multiple servers, you might
want to verify that key features are available.
• You can use function_exists() to check for the
availability of a function. function_exists()
requires a string representing a function name.
It will return true if the function can be located
and false otherwise.
Testing for the Existence of a Function
• Ex: Testing for a Function’s Existence
<html>
<head>
<title>Listing 6.14</title>
</head>
<body>
<?php
function tagWrap($tag, $txt, $func = “”) {
Testing for the Existence of a Function
if ((!empty($txt)) && (function_exists($func))) {
$txt = $func($txt);
return “<$tag>$txt</$tag>\n”;
}
}
function underline($txt) {
return “<u>$txt</u>”;
}
echo tagWrap(‘b’, ‘make me bold’);
// will print <b>make me bold</b>
Testing for the Existence of a Function
echo tagWrap(‘i’, ‘underline me too’, “underline”);
// will print <i><u>underline me too</u></i>
echo tagWrap(‘i’, ‘make me italic and quote me’,
create_function(‘$txt’, ‘return “"
$txt"”;’));
// will print <i>"make me italic and quote
me"</i>
?>
</body>
</html>
Testing for the Existence of a Function
• We define two functions, tagWrap() and
underline(). The tagWrap() function accepts
three strings: a tag, the text to be formatted,
and an optional function name. underline()
requires a single argument—the text to be
formatted.
• When we first call tagWrap() we pass it the
character b and the string bold. Because we
haven’t passed a value for the function
argument, the default value (an empty string)
is used.
Testing for the Existence of a Function
• We check whether the $func variable contains
characters and, if it is not empty, we call
function_exists() to check for a function by that
name. Of course, the $func variable is empty, so
we wrap the $txt variable and return the result.
• We call tagWrap() with the string ‘i’, some text,
and a third argument: “underline”.
function_exists() finds a function called
underline() so it calls this function and passes the
$txt argument variable to it before any further
formatting is done. The result is an italicized,
underlined string.
Testing for the Existence of a Function
• We call tagWrap(), which wraps text in
quotation entities. Of course, it would be
quicker to simply add the entities to the text to
be transformed.
• This illustrates the point that function_exists()
works as well on anonymous functions as it
does on strings representing function names.