PHP Variables Scope
In PHP, variables can be declared anywhere in the script.
The scope of a variable is the part of the script where the variable can be referenced/used.
PHP has three different variable scopes:
- local
- global
- static
Global and Local Scope
A variable declared outside a function has a GLOBAL SCOPE and can only be accessed outside a function:
Example
Variable with global scope:
Try it Yourself »
A variable declared within a function has a LOCAL SCOPE and can only be accessed within that function:
Example
Variable with local scope:
Try it Yourself »
You can have local variables with the same name in different functions, because local variables are only recognized by the function in which they are declared.
PHP The global Keyword
The global
keyword is used to access a global variable from within a function.
To do this, use the global
keyword before the variables [inside the function]:
Example
Try it Yourself »
PHP also stores all global variables in an array called $GLOBALS[index]
. The index
holds the name of the variable. This array is also
accessible from within functions and can be used to update global variables directly.
The example above can be rewritten like this:
Example
Try it Yourself »
PHP The static Keyword
Normally, when a function is completed/executed, all of its variables are deleted. However, sometimes we want a local variable NOT to be deleted. We need it for a further job.
To do this, use the static
keyword when you first declare the variable:
Example
Try it Yourself »
Then, each time the function is called, that variable will still have the information it contained from the last time the function was called.
Note: The variable is still local to the function.
PHP Exercises
The scope of a variable is defined as its range in the program under which it can be accessed. In other words, "The scope of a variable is the portion of the program within which it is defined and can be accessed."
PHP has three types of variable scopes:
- Local variable
- Global variable
- Static variable
Local variable
The variables that are declared within a function are called local variables for that function. These local variables have their scope only in that particular function in which they are declared. This means that these variables cannot be accessed outside the function, as they have local scope.
A variable declaration outside the function with the same name is completely different from the variable declared inside the function. Let's understand the local variables with the help of an example:
File: local_variable1.php
Output:
Local variable declared inside the function is: 45
File: local_variable2.php
Output:
Web development language: PHP Notice: Undefined variable: lang in D:\xampp\htdocs\program\p3.php on line 28
Global variable
The global variables are the variables that are declared outside the function. These variables can be accessed anywhere in the program. To access the global variable within a function, use the GLOBAL keyword before the variable. However, these variables can be directly accessed or used outside the function without any keyword. Therefore there is no need to use any keyword to access a global variable outside the function.
Let's understand the global variables with the help of an example:
Example:
File: global_variable1.php
Output:
Variable inside the function: Sanaya Sharma Variable outside the function: Sanaya Sharma
Note: Without using the global keyword, if you try to access a global variable inside the function, it will generate an error that the variable is undefined.
Example:
File: global_variable2.php
Output:
Notice: Undefined variable: name in D:\xampp\htdocs\program\p3.php on line 6 Variable inside the function:
Using $GLOBALS instead of global
Another way to use the global variable inside the function is predefined $GLOBALS array.
Example:
File: global_variable3.php
Output:
Sum of global variables is: 18
If two variables, local and global, have the same name, then the local variable has higher priority than the global variable inside the function.
Example:
File: global_variable2.php
Output:
Note: local variable has higher priority than the global variable.
Static variable
It is a feature of PHP to delete the variable, once it completes its execution and memory is freed. Sometimes we need to store a variable even after completion of function execution. Therefore, another important feature of variable scoping is static variable. We use the static keyword before the variable to define a variable, and this variable is called as static variable.
Static variables exist only in a local function, but it does not free its memory after the program execution leaves the scope. Understand it with the help of an example:
Example:
File: static_variable.php
Output:
Static: 4 Non-static: 7 Static: 5 Non-static: 7
You have to notice that $num1 regularly increments after each function call, whereas $num2 does not. This is why because $num1 is not a static variable, so it freed its memory after the execution of each function call.