We’ve looked at functions in the last few tutorials. Each time that you need to access data that’s outside of a function, you can declare a parameter and pass an argument to the function. But how can you access a variable outside of the function without using a parameter? I know that we haven’t looked at scope yet, but I will attempt to explain it in a few sentences.
Scope is the variable and method visibility that a certain portion of the program has access to. For example, let’s say that we declare a variable inside of a function (regular or closure). Other functions will not have access to that variable. That variable has local scope to the function where it was created. Variables can have global scope and be accessed anywhere; those variables are defined outside of the function. However, in PHP, calling a variable that has global scope inside of a function is not immediately permissible since the function will look for a local variable. Global variables must be declared as global inside of functions before they can be used. We will cover that inside of the scope article eventually. And don’t worry, this will make sense with a few examples pretty soon.
You can use both the use and global concepts to gain access to the global variable from inside of the anonymous function, but know that use gets the value of the global variable when the function is defined and global will get the value of the variable when the function is called. The use keyword can only be used with closures, not regular functions.
Before we look at an example, let’s look at the anatomy of the closure with the use keyword. If you need help with anonymous functions (closures), see my article on the topic .
<?php
$global_variable = "Dino Cajic" ;
$closure = function () use ($global_variable ) {
echo $global_variable ;
};
?>
The use keyword comes right after the function() declaration and before the opening curly brace; you will pass the global variable inside of the parentheses. Let’s jump into an example.
<?php
$name = "Dino" ;
$hello = function () use ($name ) {
echo $name ;
};
echo $name ;
$name = "Harrison" ;
echo $name ;
$hello ();
?>
PHP assigns the value string Dino to the $name variable.
The closure $hello is created. The anonymous function imports the global $name variable during the function definition. Once the function is called, it will echo the value of the $name variable.
On line 9, PHP echoes out the value inside the $name variable. Currently, $name is storing the string Dino , so Dino is displayed on the screen.
Next, on line 11, a new value, Harrison , is assigned to the $name variable.
On line 13, PHP echoes out the value inside the $name variable. Currently, $name is storing the string Harrison , so Harrison is displayed on the screen.
Finally, the closure $hello() is called.
PHP goes back to line 5 and starts executing the content that’s inside of the closure body. The echo statement is called and Dino is displayed.
We changed the value of $name to Harrison on line 11, so why is Dino displayed? Remember what I said earlier:
use gets the value of the global variable when the function is defined and global will get the value of the variable when the function is called
As PHP was reading the program, it defined the function on line 5 and copied the value of the global variable so that it can use it. It was later, on line 14, that the closure was called. The definition portion has already been performed and the value was solidified inside of the closure. That is why you get the value Dino instead of Harrison .
You could, of course, pass the global variable as an argument, but if you need to use the use function, that’s how you would do it. There are benefits to doing both as was outlined above.
<?php
$global_variable = "Dino Cajic" ;
$closure = function ($greeting ) use ($global_variable ) {
echo $greeting . " " . $global_variable ;
};
$closure ("Hello there" );
?>
Let’s look at one final example that encompasses the use keyword, the global keyword, and the parameter concept.
PHP starts off by assigning the values Dino and Harrison to the variables $name1 and $name2 respectively.
PHP defines the anonymous function. The anonymous function is declared with one parameter, $greeting . The argument will be passed during the function call. The anonymous function utilizes the use keyword to copy the value of the $name1 global variable. It then utilizes the global keyword to access the value of the $name2 global variable. Finally, it will echo out the $greeting parameter and the two global variables, $name1 and $name2.
The closure is called on line 12 and the Hello there string is passed. PHP enters the closure and displays: Hello there Dino and Harrison.
It exits the closure and continues at line 14, changing the value of $name1 from Dino to Amy.
On line 15, the value stored in $name2 is changed from Harrison to Steve.
The closure is called again on line 17. The string Hi there is passed as an argument.
PHP enters the closure and displays: Hi there Dino and Steve .
Even though both of the global variables were changed, only $name2 was updated as far the anonymous function is concerned, since use gets the value of the global variable when the function is defined, and global gets the value of the variable when the function is called .