Variables Scope In PHP
Our goal in this article is to explain how PHP variables scope impacts accessibility and lifetime of variables.
No matter what level of PHP developer you are, understanding PHP variables scope is a crucial aspect of writing an efficient and effective script.
Let’s explore PHP variable scope.
PHP Variables Scope
When it comes to PHP variables scope, variables can be declared anywhere in the script.
A variable’s scope is where it can be used or referenced in the script.
There are three different type of variable scope in PHP:
Local & Global Variable Scope
Local variables in PHP are variables that are declared within a function and are only accessible within that function. Consequently, the variable can only be used and modified within the function and cannot be accessed outside of it.
Whenever a variable with local scope exits scope, which occurs when a function ends, it is destroyed and its value is lost.
Using the $ symbol followed by the variable name, you can declare a local variable in PHP.
Local Scope variable Example:
Global variables are variables that are declared outside of any function and are accessible from anywhere in the script. Any function can access, use, and modify a global variable without having to declare it again.
You can use global variables to store information that is used by multiple functions or throughout your script.
Global Scope Variable Example:
Example using both Global and local variable scope:
In above code, a global scope variable named $a is declared with a value of 9. Then, a function named globalLocal is declared, which declares its own local scope variable named $a with a value of 11.
When the function is called, it prints the value of its local scope variable $a, which is 11. This is done by concatenating the string “Variable a from inside the function” with the value of $a using the . operator and printing the result using echo.
After the function is called, the value of the global scope variable $a is printed, which is 9. This is done by concatenating the string “Variable a from outside the function” with the value of $a using the . operator and printing the result using echo.
The final output of the code will be two paragraphs, each containing the value of a different $a variable.
Global Keyword In PHP
Using the global keyword allows you to access a global variable within a function.
You can achieve this by using the global keyword before the variable (inside the function):
PHP Global Keyword Example: 1
PHP Global Keyword Example: 2
Likewise, PHP stores all global variables in an array named $GLOBALS[index].
Variable names are stored in the index. A global variable can be directly updated from within functions using this array.
Rewriting the above example would be as follows:
PHP Global Keyword Example: 3
PHP Global Keyword Example: 4
Static Keyword In PHP
As soon as a function has been completed/executed, all its variables are deleted.
In some cases, we don’t want a local variable to be deleted.
We need it later. To perform some further jobs.
To accomplish this, declare the variable with the static keyword:
PHP Static Keyword Example: 1
PHP Static Keyword Example: 2
This means that each time the function is called, the variable will still contain the same information as before.
Knowing how PHP variables scope work and how to use them is essential. When you understand the default scope of variables inside and outside of functions, and when the global keyword is needed, you can write PHP code that works as intended. PHP applications can be developed robustly and reliably when you have this knowledge.