• Aucun résultat trouvé

Understanding Scope

Dans le document PHP and MySQL ® Web Development (Page 188-191)

}

This function reports the number of parameters passed to it and prints out each of them.

The func_num_args()function returns the number of arguments passed in.The

func_get_args()function returns an array of the arguments. Alternatively, you can access the arguments one at a time using the func_get_arg()function, passing it the argument number you want to access. (Arguments are numbered starting from zero.)

Understanding Scope

You might have noticed that when we needed to use variables inside a required or included file, we simply declared them in the script before the require()or include() statement.When using a function, we explicitly passed those variables into the function partly because no mechanism exists for explicitly passing variables to a required or included file and partly because variable scope behaves differently for functions.

07_0672329166_ch05.qxd 9/3/08 1:14 PM Page 150

151 Understanding Scope

A variable’s scope controls where that variable is visible and usable. Different pro-gramming languages have different rules that set the scope of variables. PHP has fairly simple rules:

n Variables declared inside a function are in scope from the statement in which they are declared to the closing brace at the end of the function.This is called function scope.These variables are called local variables.

n Variables declared outside functions are in scope from the statement in which they are declared to the end of the file, but not inside functions.This is called global scope.

These variables are called global variables.

n The special superglobal variables are visible both inside and outside functions. (See Chapter 1, “PHP Crash Course,” for more information on these variables.)

n Using require()and include()statements does not affect scope. If the statement is used within a function, function scope applies. If it is not inside a function, glob-al scope applies.

n The keyword globalcan be used to manually specify that a variable defined or used within a function will have global scope.

n Variables can be manually deleted by calling unset($variable_name). A variable is no longer in scope if it has been unset.

The following examples might help to clarify scope further.

The following code produces no output. Here, we declare a variable called $var inside the function fn(). Because this variable is declared inside a function, it has func-tion scope and exists only from where it is declared until the end of the funcfunc-tion.When you again refer to $varoutside the function, a new variable called $varis created.This new variable has global scope and will be visible until the end of the file. Unfortunately, if the only statement you use with this new $varvariable is echo, it will never have a value.

The following example is the inverse. Here, you declare a variable outside the function and then try to use it within a function:

<?

function fn() {

echo "inside the function, \$var = ".$var."<br />";

$var = "contents 2";

echo "inside the function, \$var = ".$var."<br />";

}

152 Chapter 5 Reusing Code and Writing Functions

$var = "contents 1";

fn();

echo "outside the function, \$var = ".$var."<br />";

The output from this code is as follows:

inside the function, $var =

inside the function, $var = contents 2 outside the function, $var = contents 1

Functions are not executed until they are called, so the first statement executed is $var

= ‘contents 1’;.This statement creates a variable called $var, with global scope and the contents “contents 1”.The next statement executed is a call to the function fn(). The lines inside the statement are executed in order.The first line in the function refers to a variable named $var.When this line is executed, it cannot see the previous $var that was created, so it creates a new one with function scope and echoes it.This creates the first line of output.

The next line within the function sets the contents of $varto “contents 2”. Because you are inside the function, this line changes the value of the local $var, not the global one.The second line of output verifies that this change worked.

The function is now finished, so the final line of the script is executed.This echo statement demonstrates that the global variable’s value has not changed.

If you want a variable created within a function to be global, you can use the keyword globalas follows:

function fn() { global $var;

$var = "contents";

echo "inside the function, \$var = ".$var."<br />";

} fn();

echo "outside the function, \$var = ".$var."<br />";

In this example, the variable $varis explicitly defined as global, meaning that after the function is called, the variable will exist outside the function as well.The output from this script follows:

inside the function, $var = contents outside the function, $var = contents

Note that the variable is in scope from the point in which the line global $var;is executed.You could declare the function above or below where you call it. (Note that function scope is quite different from variable scope!) The location of the function dec-laration is inconsequential; what is important is where you call the function and there-fore execute the code within it.

07_0672329166_ch05.qxd 9/3/08 1:14 PM Page 152

153

Dans le document PHP and MySQL ® Web Development (Page 188-191)

Documents relatifs