The variable in the PHP scope can be defined as the variable that already defined in anywhere and can be accessed from another place.
Table Of Contents
The PHP variables function within a unified scope, allowing you to retrieve variables from another PHP file.
Anyway, There are three scopes in PHP, which are local scope, global scope and static.
The following section, shows you an example of the place of each one
<?php
$var = 10; // => Variable Scope
function func() {
echo $var; // Used as a refs for local variable
}
func();
Let’s focus in each one in-depth. But before that, you have to understand what scope is in PHP.
What Does Mean Scope Word in PHP
As I mentioned, the scope is a variable that can be defined in a place and invoked as a reference in another place.

If you see this puzzle, you will find 4 layers installed correctly, but there is an additional layer would not work in its new position because it invoked incorrectly or invoked in a wrong position.
This process is called the variable scope in PHP.
To understand what I mean, you have to change the way of thinking and change the layers of this puzzle into variables and functions in PHP. And let me know does that work with you.
Define a variable and invoke it as a reference in another place, this is the concept of the scope in any programming language.
Anyway, in the following sections, I will focus on each type with examples.
Using the Local Variable in the Scope
The local scope variable is the variable that can be defined inside a code block, so you can access it in the same block. For example.
<?php
function func() {
$print = "Welcome to CodedTag tutorial.";
echo $print;
}
func();
The $print variable is a local scope variable, so you can use it in any place in the function. However, it is not possible to use a local variable as a reference outside the function, since local variables can only be accessed within the same block.
It wouldn’t be used outside the block. For example.
<?php
function func() {
$print = "Welcome to CodedTag tutorial.";
}
func();
echo $print; // error here
In this example I used the reference of the local variable $print outside the function. This will occur the following error.
Warning: Undefined variable $print in /index.php on line 9
In the following section, you will understand what is the global keyword and how to use the variable in the global scope.
Getting the Variable from the Scope by using Global Keyword
The global is a defined property in PHP allows you to use the variable that are defined outside the block ( outside the local scope) which is written in the general PHP script.
For example.
<?php
$print = "CodedTag Tutorials.";
function func() {
global $print;
echo $print;
}
func();
Also, you can use this word beside many global scope variables like this : global $x, $y, $z
. For example.
<?php
$username = "Montasser";
$email = "[email protected]";
function contact() {
global $username, $email;
echo "Hello {$username}, if you have any question contact us at this email : {$email}";
}
contact();
You can also use another way $GLOBALS[….] to invoke the variable as globally. I will use the same previous example.
<?php
$username = "Montasser";
$email = "[email protected]";
function contact() {
echo "Hello {$GLOBALS['username']}, if you have any question contact us at this email : {$GLOBALS['email']}";
}
contact();
Using the Static Variables
The static variables allow us to use it as a global scope in the local function scope.
Anyway, the following example shows you a function contains an action to increase the $x variable. Let’s see how it does work.
<?php
function increment() {
$x = 0;
echo $x;
$x++;
}
increment();
increment();
increment();
?>
The output will be as the below.
0 0 0
There is no any change in the variable, but PHP has a keyword to allow the scope to keep the last variable value in the local scope, which is the “static” keyword.
I will use the same previous example.
<?php
function increment() {
static $x = 0;
echo $x;
$x++;
}
increment();
increment();
increment();
?>
The output will be as the following.
0 1 2
You can also use the static keyword with recursive functions.
<?php
function counter() {
static $y = 0;
echo $y;
$y++;
if( $y <= 20 ) {
counter();
}
}
counter();
?>
This will print: 01234567891011121314151617181920