Home »
PHP
PHP Variable Scope
By Shahnail Khan Last updated : November 30, 2023
Variable scope is a fundamental concept in programming that determines where and how a variable can be accessed within a program. In PHP, understanding variable scope is crucial for writing clean, organized, and bug-free code. This tutorial explores the PHP variable scopes, providing a comprehensive explanation of the different types of scopes and their implications for code execution.
Different Types of Variable Scope
There are primarily three types of variable scopes namely, local, global, and static. Let's understand each of them one by one with suitable examples.
PHP Local Scope
Variables declared within a function or a block of code have a local scope. They are accessible only within that specific function or block.
Example
This example demonstrates the local scope of PHP variables.
<?php
function Function1()
{
$localVariable = "This is local variable!";
echo $localVariable;
}
Function1();
?>
Output:
This is local variable!
This code defines a function named Function1 and declares a local variable named $localVariable within the function. The value "This is local variable!" is assigned to the $localVariable. The function then displays the value of $localVariable to the output.
PHP Global Scope
Variables declared outside of any function or code block have a global scope. They can be accessed from any part of your code, including within functions.
Example
This example demonstrates the global scope of PHP variables.
<?php
$globalVariable = 10;
function accessGlobalVariable()
{
global $globalVariable;
echo "The global variable is $globalVariable.";
}
accessGlobalVariable();
?>
Output:
The global variable is 10.
In this example, the global keyword is used within the accessGlobalVariable() function to access the global variable $globalVariable declared outside the function.
PHP Static Scope
Static variables maintain their values between function calls but have a local scope. They are declared using the static keyword within a function.
Example
This example demonstrates the static scope of PHP variables.
<?php
function countClicks()
{
static $count = 0;
$count++;
echo "Click count: $count . <br>";
}
countClicks();
countClicks();
countClicks();
?>
Output:
Click count: 1 .
Click count: 2 .
Click count: 3 .
In this example, the variable $count is declared as a static variable using the static keyword. The countClicks() function counts the number of times it is called and updates the $count variable accordingly. The updated value of the $count remains stored even when the function is called again.
Difference Between PHP Local, Global, And Static Scope
The table given below differentiates the three variables' scope -
Scope Type | Declaration | Visibility |
Local Scope |
Declared inside a function or block of code. |
Limited to the specific function or block. |
Global Scope |
Declared outside any function or block of code. |
Accessible from any part of the code, including within functions. |
Static Scope |
Declared with the static keyword inside a function. |
Limited to the specific function. Retains its value between function calls. |
PHP Variable Scope FAQs
1. What is variable scope?
Variable scope determines where and how a variable can be accessed within a program.
2. How can I access a global variable inside a function?
To access a global variable within a function, use the global keyword followed by the variable name. This ensures the function can read and modify the global variable.
3. What is the difference between local, global, and static variables?
Local variables are only accessible within the block of code where they are declared, global variables are accessible from anywhere within the program, and static variables maintain their value between function calls.