DONATE Hyvor Developer
PHP
PHP TUTORIALPHP IntroductionPHP InstallationPHP Hello WorldPHP Basic SyntaxPHP CommentsPHP VariablesPHP Variable ScopePHP ConstantsPHP OutputPHP Data TypesPHP Type CastingPHP OperatorsPHP ConditionalsPHP Shorthand ConditionalsPHP LoopsPHP Loop Control StructuresPHP FunctionsPHP StringsPHP ArraysPHP Superglobal VariablesPHP in HTMLPHP RegexRegex IntroductionRegex PCRE SyntaxPHP PREG FunctionsPHP FormsPHP Forms IntroductionPHP Forms CreatingPHP Forms SecurityPHP Forms ValidationPHP Forms Required InputsPHP Forms StickyPHP Forms Advanced ValidationPHP Forms Finishing

PHP Variable Scope

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

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.

There are three types of scopes in PHP.

  • Global Scope
  • Local Scope
  • Static Scope

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


<?php
$number = 10; // global scope

echo $number; // outputs 10

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

Run Example ››

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


<?php

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

// $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.

PHP Global Keyword Example


<?php
$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().

2. PHP $GLOBALS Array

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


<?php
$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


<?php
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.

0
59