Functions in PHP
Variable Scope
If you are migrating
to PHP from another programming language, you probably are already familiar
with the concept of variable scope; however PHP handles it slightly different
from other languages.
For those of you unfamiliar with the concept: Scope determines where within
a script a variable can be used. In PHP, variables have a single, global
scope by default, meaning that they are available for use within the main
script and any included or required files, but not within functions.
By the same token, a variable created or altered within the function would
not automatically be available to the entire script, because it was created
within the local scope of the function.
Obviously, you need to be able to move variables from the scope of the
main script to the scope of the function in order to work with them. You
also be able to take those variables back into the scope of the main script.
To do this, we pass variables to, and return them from, the function.
Since we have already taken a brief look at returning variables, let's
look at several of the more common ways to move a variable from the global
scope of the script to the local scope of a function.
1. Pass the variable
as an argument to the function
This is the easiest
and most common method. To illustrate, let's look at our simple greeting
script again. Rather than passing the name directly to the script as an
argument, it will be defined as a variable first:
<?php $name = "Billy Joe Bob"; function say_hi($name) { return "Hi there $name!"; } print say_hi($name); ?> |
It is important to keep in mind that when you pass a variable like this,
you are actually copying the value of the original variable to a new variable
within the local scope of the function. This means that even if you use
the same name, changes to the variable within the function will not affect
the variable outside the function. Take a look at this example to see
this rule in action:
<?php $x = 1; function add($x) { $y++; print $y; } add($x); print $x; ?> |
2. Pass the variable as
a reference
References alone are an expansive topic to cover, so we will just
take a quick look at how they can be used with functions. This method
is very similar to the first; however instead of copying the value of
a variable to a new one within a function, you are pointing the new variable
name to the same value.
Technically, two different variable names, one within the global scope
of the script, and the other within the local scope of the function will
both point to the same value. If this seems confusing, try the following
sample and compare the results of this script to the previous one. Notice
the "&" before the variable name in the function definition, which
marks it as a reference:
<?php $x = 1; function add(&$y) { $y++; } add($x); print $x; ?> |
Since we are altering the actual value of $x from within the function,
there is no need to even return anything from the function. When $x is
printed out, its value will have increased by one.
3. Declare the variable
global within the function or access it via the GLOBALS array
Declaring a variable
as global has much the same effect as using a reference, but the reasoning
behind it is a little different. Rather than creating a new variable name
that references the same value as another, we are telling the script to
use the exact same variable and value inside the function. You can make
more than one variable global just by separating them by commas.
<?php $x = 1; function add() { global $x; $x++; } add(); print $x; ?> |
You can also access variables
from the global scope of the script by using PHP's GLOBALS Array. This
is an Associative Array which allows you to access each variable in the
form of $GLOBALS["varname"]:
<?php $x = 1; function add() { $GLOBALS["x"]++; } add(); print $x; ?> |
Finally
By now, you should have a taste of the basics of functions: what they
are, how to use them, and how to create your own. You should also have
a basic understanding of Variable Scope and how it comes into play when
writing functions.
In the next article,
we will use functions extensively to manipulate Arrays and explore how
they work.
Stay Tuned!
Things to Remember:
|
Liz Fulghum currently lives in Annapolis, MD where she works as a web designer for a custom shirt retailer. Liz was convinced to try PHP as an alternative to Perl; she has been a fan ever since and frequently works as a freelance developer.
# # #
Page 3 of 3
This article was originally published on January 17, 2002