The heart of PHP programming is,
arguably, the function. The ability to encapsulate any piece of code in a way
that it can be called again and again is invaluable—it is the cornerstone of
structured procedural and object oriented programming.
In this blog, we focus on the
various aspects of creating and managing functions from within PHP
scripts—therefore, this blog is about writing functions, rather than using
them.
Basic
Syntax
Function syntax is, at its most
basic, very simple. To create a new function, we simply use the keyword
function, followed by an identifier, a pair of parentheses and braces:
function name() { }
PHP function names are not
case-sensitive. As with all identifiers in PHP, the name must consist only of
letters (a-z), numbers and the underscore character, and must not start with a
number.
To make your function do something,
simply place the code to be execute between the braces, then call it.
function hello()
{
echo "Hello
World!";
}
hello(); // Displays
"Hello World!"
Returning
Values
All functions in PHP return a
value—even if you don’t explicitly cause them to. Thus, the concept of “void”
functions does not really apply to PHP. You can specify the return value of
your function by using the return keyword:
function hello()
{
return "Hello
World"; // No output is shown
}
$txt = hello(); //
Assigns the return value "Hello World" to $txt
echo hello(); //
Displays "Hello World"
Naturally, return also allows you to
interrupt the execution of a function and exit it even if you don’t want to
return a value:
function hello($who)
{
echo "Hello $who";
if ($who == "World") {
return; // Nothing else in the function will
be processed
}
echo ", how are you";
}
hello("World");
// Displays "Hello World"
hello("Reader")
// Displays "Hello Reader, how are you?"
Note, however, that even if you
don’t return a value, PHP will still cause your function to return NULL.
Functions can also be declared so
that they return by reference; this allows you to return a variable as the
result of the function, instead of a copy (returning a copy is the default for
every data type except objects). Typically, this is used for things like resources
(like database connections) and when implementing the Factory pattern.
However, there is one caveat: you
must return a variable—you cannot return an expression by reference, o r use an
empty return statement to force a NULL return value:
function
&query($sql)
{
$result = mysql_query($sql);
return $result;
}
// The following is
incorrect and will cause PHP to emit a notice when called.
function
&getHello()
{
return "Hello
World";
}
// This will also
cause the warning to be issued when called
function &test()
{
echo ’This is a test’;
}
Variable
Scope
PHP has three variable scopes: the
global scope, function scope, and class scope. The global scope is, as its name
implies, available to all parts of the script; if you declare or assign a value
to a variable outside of a function or class, that variable is created in the
global scope.
However, any time you enter a
function, PHP creates a new scope—a “clean slate” that, by default, contains no
variable and that is completely isolated from the global scope. Any variable defined
within a function is no longer available after the function has finished
executing. This allows the use of names which may be in use elsewhere without
having to worry about conflicts.
$a = "Hello
World";
function hello()
{
$a = "Hello
Reader";
$b = "How are
you";
}
hello();
echo $a; // Will
output Hello World
echo $b; // Will emit
a warning
There are two ways to access
variables in the global scope from inside a function; the first consists of
“importing” the variable inside the function’s scope by using the global
statement:
$a =
"Hello";
$b =
"World";
function hello()
{
global $a, $b;
echo "$a $b";
}
hello(); // Displays
"Hello World"
You will notice that global takes a
comma-separated list of variables to import—naturally, you can have multiple
global statements inside the same function.
Many developers feel that the use of
global introduces an element of confusion into their code, and that
“connecting” a function’s scope with the global scope can easily be a source of
problems. They prefer, instead, to use the $GLOBALS superglobal array, which
contains all the variables in the global scope:
$a =
"Hello";
$b =
"World";
function hello()
{
echo $GLOBALS[’a’] .’
’. $GLOBALS[’b’];
}
hello(); // Displays
"Hello World"
Passing
Arguments
Arguments allow you to inject an
arbitrary number of values into a function in order to influence its behaviour:
function hello($who)
{
echo "Hello
$who";
}
hello("World");
/
*
Here we pass in the
value, "World", and the function displays "Hello World"
*
/
You can define any number of
arguments and, in fact, you can pass an arbitrary number of arguments to a
function, regardless of how many you specified in its declaration. PHP will not
complain unless you provide fewer arguments than you declared.
Additionally, you can make arguments
optional by giving them a default value. Optional arguments must be right-most
in the list and can only take simple values—expressions are not allowed:
function hello($who =
"World")
{
echo "Hello
$who";
}
hello();
/
*
This time we pass in
no argument and $who is assigned "World" by default.
*
/
Variable-length
Argument Lists
A common mistake when declaring a
function is to write the following:
function f ($optional
= "null", $required)
{
}
This does not cause any errors to be
emitted, but it also makes no sense whatsoever—because you will never be able
to omit the first parameter ($optional) if you want to specify the second, and
you can’t omit the second because PHP will emit a warning. In this case, what
you really want is variable-length argument lists—that is, the ability to
create a function that accepts a variable number of arguments, depending on the
circumstance. A typical example of this behaviour is exhibited by the printf() family
of functions.
PHP provides three built-in
functions to handle variable-length argument lists:
func_num_args(), func_get_arg() and
func_get_args(). Here’s an example of how they’re used:
function hello()
{
if (func_num_args()
> 0) {
$arg = func_get_arg(0);
// The first argument is at position 0
echo "Hello
$arg";
} else {
echo "Hello
World";
}
}
hello("Reader");
// Displays "Hello Reader"
hello(); // Displays
"Hello World"
You can use variable-length argument
lists even if you do specify arguments in the
function header. However, this won’t
affect the way the variable-length argument list
functions behave—for example, func_num_args()
will still return the total number of
arguments passed to your function,
both declared and anonymous.
function
countAll($arg1)
{
if (func_num_args() ==
0) {
die("You need to
specify at least one argument");
} else {
$args = func_get_args();
// Returns an array of arguments
// Remove the defined
argument from the beginning
array_shift($args);
$count = strlen
($arg1);
foreach ($args as
$arg) {
$count +=
strlen($arg);
}
}
return $count;
}
echo
countAll("foo", "bar", "baz"); // Displays ’9’
Passing
Arguments by Reference
Function arguments can also be
passed by reference, as opposed to the traditional by-value method, by prefixing
them with the by-reference operator &. This allows your function to affect
external variables:
function
countAll(&$count)
{
if (func_num_args() ==
0) {
die("You need to
specify at least one argument");
} else {
$args = func_get_
args(); // Returns an
array of arguments
// Remove the defined
argument from the beginning
array_shift($args);
foreach ($args as
$arg) {
$count +=
strlen($arg);
}
}
}
$count = 0;
countAll($count,
"foo", "bar", "baz"); // $count now equals 9
0 Comment to " PHP Functions (Basic Syntax, Returning Values, Variable Scope) "
Post a Comment