PHP: Scope
Derek Bridge
Department of Computer Science,
University College Cork
PHP: Scope
Aims:
- to understand some common errors involving programmer-defined functions
- to learn about scope, and the difference between formal parameters,
local variables, global variables and superglobal variables
Common error I: invoking an undefined function
function output_paragraph( $text )
{
echo "<p>{$text}</p>";
}
outputParagraph( 'Today is ' . Date('d/m/y') );
Common error II
A novice programmer incorrectly thinks that the radius (5) is available inside
this function
function get_circle_area()
{
return 3.14 * $radius * $radius;
}
$radius = 5;
$area = get_circle_area();
echo "<p>The area of the circle is {$area}</p>";
Common error III
A novice programmer incorrectly thinks that the circumference (31.4) is available
outside of this function
function get_circle_circumference( $diameter )
{
$circumference = 3.14 * $diameter;
}
echo '<p>';
echo 'The circumference is ';
get_circle_circumference( 10 );
echo $circumference;
echo '</p>';
Variables in PHP
- If you do not use functions, any variables you create can be used anywhere
in your script
- But if you define your own functions, you must distinguish four kinds of variable
- a function's formal parameters
- a function's local variables
- your script's global variables
- PHP's superglobal variables
- These differ in
- where they are created
- which hence determines their scope
- The scope of a variable is the range of statements in the program in which
the variable's name is known
Formal parameters and local variables
- A function's formal parameters
- as we know, these are created when the function is invoked
- scope: the function's body
- A function's local variables
- variables which are created inside a function
- scope: from the statement in which they are created to the end
of the function's body
Global variables and superglobal variables
- A script's global variables
- variables which are created outside a function
- scope: from the statement in which they are created to the end
of the script but not inside functions
- PHP's superglobal variables, e.g.
$_GET
, $_POST
- as we have seen, these are special PHP variables which you don't create
- scope: the whole script, both inside and outside functions
Class exercise
Draw boxes on this program to show the scope of each variable
$v1 = 10;
$v2 = $v1 / 2;
echo "<p>{$v1} {$v2}</p>";
function f( $x1 )
{
echo "<p>{$x1}</p>";
$x2 = 25;
$x3 = $x2 * $x1;
echo "<p>{$x2} {$x3}</p>";
}
$v3 = 2;
f(6);
f($v3 + $v2);
echo "<p>{$v3}</p>";
Using a variable outside its scope
- So what happens if you try to use a variable outside of its scope?
- In some programming languages, you get a fatal error message (either
at compile-time or run-time, depending on the programming language)
- PHP gives no error/warning message: it simply assumes that this must
be a different variable (although there may be a warning that this
new variable is uninitialized)
Class exercise: what is the output of the following?
function func1()
{
echo $x;
}
$x = 10;
func1();
Class exercise: what is the output of the following?
function func2()
{
$x = $x + 1;
}
$x = 5;
func2();
echo $x;
Class exercise: what is the output of the following
function larger( $x, $y )
{
$maximum = $y;
if ( $x > $y )
{
$maximum = $x;
}
return $maximum;
}
echo '<p>';
echo larger( 3, 2 );
echo ' ';
echo $maximum;
echo '</p>';
Class exercise
- Rewrite the
get_circle_area
and get_circle_circumference
examples so that they work as the programmer presumably intended
Pointers for advanced students
- Look up the keyword
global
- it turns a local variable into a global variable
- generally avoid it: it makes it harder to reuse your functions in
other scripts because they are tied into the current script
- Look up the keyword
static
- it enables a local variable to retain its value between invocations
- generally avoid: it makes your code harder to understand (it means
that functions can have side-effects and so multiple invocations even
with the same actual parameters may give different results)