Skip to main content

Variable Scope in PHP Programming

While doing PHP code review sometime I have observed some confusion related to variables. Sometime confusions are related to basics of variable scoping and sometime with good practices. In this post I will try to explain about variable scope in php programming. Also I will put my own thought on what should we avoid to do and what should we prefer.
I am writing this post for newcomers so to read this post you should had only basic understanding of PHP.
Variable Scope in PHP Programming

What is Variable Scope in PHP Programming

I am considering the heading “what is variable scope in PHP Programming” as a very odd topic. But considering that the PHP has some nice way of handling the variables, I have added this topic in my post. In PHP use of variable is very flexible and easy to use. To understand in better way let us explore the scope of variable in following 3 aspect:

  1. Normal/Flat Scripting : Classic way of writing script. Include the file and writing free form of scripting.
  2. Procedural Programming : Using functions and include file.
  3. Object Oriented Programming : Complete object oriented programming.

Variable scope in normal/Flat Scripting

Best thing about PHP is that you can quickly write a flat PHP script to dynamically generate your webpage or to automate any job to run it through command line.

In flat scripting if you will write any variable then it will be globally available everywhere in your script. Even in the included files also. The scope of the variable will be within the whole context.

Following are some example:
a.php:
<?php
$a = 10;
require 'b.php';

Now variable $a will be available in b.php as well.  You can use $a directly in b.php. For example(b.php):
<?php
echo $a;

So if you are writing a script without any function and class and declaring a variable in PHP then the scope of that variable will be global and will be available in complete script. The variable will be available in all included files, but it will not be directly available inside of any function or class.

Point to Think: Do you think that one should declare variable and assign a value in one file and use in other included file in very lower hierarchy. For me I never prefer to do that. I always think that will create pain for the person who will take code for your to maintain. Do you think the same?

You can create variable in any programming block like if/else or foreach or while and use outside the block. For example:
$test =1;
if($test == 1){
$new_var = 1; //$new_var is created and assigend value in if block
}
echo $new_var; //It will print 1

But what will happen if the code will be like below
$test =2;
if($test == 1){
$new_var = 1; //$new_var is created and assigend value in if block
}
echo $new_var; //It will print 1

So script will throw notice “Undefined variable $new_var”

Again a point to think that, should we use variable like this?

Variable sope in Procedural PHP Programming

Again one more odd topic I have chosen. Because PHP provides some nice way to write object oriented code and most people write object oriented code. Is not it? But I have taken the procedural programming head to explain what is variable behaviour in procedural PHP programming. So let us explore.
If any variable is declared inside the function then the variable will only be available inside the function only, one can not access it outside the function. Means varialbe will available locally and usually we call these variable as local variable.
function test(){
$a = 5;
echo $a; //Will print 5
}
test();
echo $a; //Will throw notice at this line

So in above method echo $a outside the script will throw notice for undefined variable. Now let us understand the same situation in other example:
$a = 1;
function test(){
$a = 5;
echo $a; //Will print 5
}
test();
echo $a;//will print 1

So the one which is declare outside f= function  in the script will be available for outside and one which is declare inside will be available in inside.
Another situation, now what happens with variable declare out side of function into inside of the function. For example  :
$a = 1;
function test(){
echo $a; //Will thorw notic at this line
}
test();
echo $a;//will print 1

So the echo $a inside the function test will throw notice. If variable is declared in the global script then it can not be accessed/used directly into function. But is there any way to access it inside? Then answer is Yes, it can be accessed using global keyword. For example :
$a = 1;
function test(){
global $a;
echo $a; //will print 1
}
test();
echo $a;//will print 1

Will explain global in later of this post.

Variable Scope in PHP Object Oriented Programming

Yeah! Now got topic of latest trend which is object oriented programming. These days every one is talking about Object Oriented programming. Similarly like function you can not access globally declared variable in side the class method directly. To access such type of variable you need to use global keyword or $GLOBALS variable. We will check both in later part. To know how local variable behaves inside the class you may refer to my following post:

  1. Class and Object in PHP
  2. Visibility in PHP Classed

Till now we have discussed on various aspect of variable scope of PHP programming but there are some feature or way in php by which you can change the variable scope to use it in your own way. Now let us explore those feature which impacts variable scope in PHP programming.

Below are few things which can be used to alter the variable scope in PHP programming

  1. global Keyword
  2. $GLOBALS variable

Now let us explore all these one by one.

global keyword

Global keyword is used to access the global scope variable in any local function. So let us refer the previous example again:
$a = 1;
function test(){
global $a;
echo $a; //will print 1
}
test();
echo $a;//will print 1
Here we have used $a global variable in local area which is function test and now the global variable $a variable is accessible with-in the function. Keyword global can be used for more than one variable as well. For example:
$a = 1;
$b = 10;
function test(){
global $a, $b;
echo $a;
echo $b;
}
test();

Using global keyword if variable scope made available locally then we can change its value also in local and it will be available globally. For example
$a = 1;
$b = 10;
function test(){
global $a, $b;
$a = 5;
$b = 7;
}
test();
echo $a;//will print 5
echo $b;//will print 7

So global keyword is very useful in altering scope of variable in PHP programming.

$GLOBALS Variable

$GLOBALS is variable which references all variable of the script which are available in global scope. For example:
$a = 1;
$b = 10;
function test(){

echo $GLOBALS[‘a’];
echo $GLOBALS[‘b’];
}
test();

In Globals you can edit your variable as well:
$a = 1;
$b = 10;
function test(){
$GLOBALS['a'] = 5;
$GLOBALS['b'] = 3;
}
test();
echo $a; // print 5
echo $b; //print 3

Download all code used in the Post

For Further Reading you may refer following URL
http://php.net/manual/en/language.variables.scope.php
http://php.net/manual/en/language.variables.superglobals.php

Support Me by Sharing This Article

Ankur Kumar Singh

I am a PHP programmer having some knowledge about Linux. I am always interested in web development and knowledge sharing. I am full time tech evangelist part time human being. :-)

Leave a comment/Ask Question

shares