PHP TUTORIALPHP TutorialPHP InstallationPHP Hello WorldPHP Basic SyntaxPHP CommentsPHP VariablesPHP Variable ScopePHP ConstantsPHP StringsPHP OutputPHP Data TypesPHP Type CastingPHP OperatorsPHP ConditionalsPHP Shorthand ConditionalsPHP LoopsPHP Loop Control StructuresPHP FunctionsPHP String FunctionsPHP ArraysPHP Superglobal VariablesPHP in HTMLPHP AdvancedPHP Include and RequirePHP HTTP & HTTPSPHP RegexRegex IntroductionRegex PCRE SyntaxPHP PREG FunctionsPHP FormsPHP Forms IntroductionPHP Forms CreatingPHP Forms SecurityPHP Forms ValidationPHP Forms Required InputsPHP Forms StickyPHP Forms Advanced ValidationPHP Forms FinishingPHP OOPPHP OOP IntroductionPHP OOP ClassesPHP OOP PropertiesPHP OOP ObjectsPHP OOP MethodsPHP OOP $this KeywordPHP OOP Constructors and DestructorsPHP OOP VisibilityPHP OOP InheritancePHP OOP Abstract Classes and MethodsPHP OOP InterfacesPHP OOP TraitsPHP OOP ConstantsPHP OOP StaticPHP OOP NamespacesPHP OOP Autoloading

PHP Variable Scope

Scope refers to the visibility of variables. In other words, which part of the program can use that variable.

In PHP, variable scope goes with functions. Before learning variable scope you need to understand the basis of functions. For now, keep in mind that, a function is a combination of instructions. We will learn more about functions later in the PHP functions tutorial.

This chapter would be a mess for you if you are a beginner and you don't know what are functions. Don't think much of that. Read this slightly and continue other chapters and right after you finish the functions chapter, visit this again.

There are three types of scopes in PHP.

PHP Global Scope

A variable declared in the main flow of the code (not inside a function) has a global scope. These variables can't be used inside functions.

PHP Global Scope Example

$number = 10; // global scope

echo $number; // outputs 10

function num() {
	echo $number; // $number is undefined here

PHP Local Scope

A variable declared inside a function has a local scope. This variable is unique to this function and can only be accessed within the function. You can define different variables with the same name in different functions.

These variables are deleted and no longer available after the execution of the function.

PHP Local Scope Example


function hello() {
	$txt = 'Hello World'; // local scope
 	echo $txt; // $txt can be used here

// $txt cannot be used outside

Run Example ››

PHP Global Variables in Local Scope

There are two ways to access global variables within a function.

  • Using the global keyword
  • Using the $GLOBALS array

1. PHP global Keyword

You can use global keyword inside a function to access global variables. Before using variables, add global keyword followed by comma-separated variable names you need to use inside the function.

PHP Global Keyword Example

$x = 'Hyvor';
$y = 'Developer';

function websiteName() {
	global $x, $y; 
	echo $x, $y;

websiteName(); // outputs HyvorDeveloper

Run Example ››

If you remove global $x, $y; part from the code, it will not work because $x and $y variables are not defined within the function websiteName().


PHP saves all the global variables in PHP-defined $GLOBALS array. The array key contains the variable name. The array value contains the variable value. The previous example can be written as follows.

PHP $GLOBALS Array Example

$x = 'Hyvor';
$y = 'Developer';

function websiteName() {
	echo $GLOBALS['x'], $GLOBALS['y'];

websiteName(); // outputs HyvorDeveloper

Run Example ››

Tip: Array is another data type if PHP which we will discuss in the PHP Arrays tutorial.

PHP Static Scope

As we discussed earlier, local scope variables are deleted after the end of the execution of the function. But, sometimes we need to keep the variable alive.

To use this feature, add static keyword before the variable when declaring it.

PHP Static Variable Example

function test() {
	static $number = 0; // declare static variable
	echo $number . '<br>'; // echo number with line break
	$number = $number + 5; // add five to $number

test(); // 0
test(); // 5
test(); // 10

Run Example ››

Each time the function is called, $number variable has the value of the last assignment.

Visit PHP Help group to get some help from experts.
Profile Picture
Supun Kavinda
I'm the Founder of Hyvor, Web Developer, Physics Lover, Flutist, and a Table Tennis Player.
My Websites