Variables Scope In PHP

Our goal in this article is to explain how PHP variables scope impacts accessibility and lifetime of variables.

No matter what level of PHP developer you are, understanding PHP variables scope is a crucial aspect of writing an efficient and effective script.

Let’s explore PHP variable scope.

PHP Variables Scope

When it comes to PHP variables scope, variables can be declared anywhere in the script.

A variable’s scope is where it can be used or referenced in the script.

There are three different type of variable scope in PHP:

  1. local
  2. global
  3. static


Local & Global Variable Scope

Local variables in PHP are variables that are declared within a function and are only accessible within that function. Consequently, the variable can only be used and modified within the function and cannot be accessed outside of it.

Whenever a variable with local scope exits scope, which occurs when a function ends, it is destroyed and its value is lost.

Using the $ symbol followed by the variable name, you can declare a local variable in PHP.

Local Scope variable Example: 

<?php function localVariable() { $a = 9; // local scope variable echo "<p>Variable a from inside the function is: $a</p>"; } localVariable(); // a outside the function will generate an error echo "<p>Variable a from outside the function is: $a</p>"; ?>

Global variables are variables that are declared outside of any function and are accessible from anywhere in the script. Any function can access, use, and modify a global variable without having to declare it again.

You can use global variables to store information that is used by multiple functions or throughout your script.

Global Scope Variable Example: 

<?php $a = 9; // global scope variable function globalVariable() { // a inside the function will generate an error echo "<p>Variable a from inside the function is: $a</p>"; } globalVariable() ; echo "<p>Variable a from outside the function is: $a</p>"; ?>

Example using both Global and local variable scope:

<?php $a = 9; // global scope variable function globalLocal() { $a = 11 // local scope variable echo "<p>Variable a from inside the function is: $a</p>"; } echo globalLocal() ; //This prints the local variable echo "<p>Variable a from outside the function is: $a</p>"; //This prints the global variable ?>

Example Explanation:

In above code, a global scope variable named $a is declared with a value of 9. Then, a function named globalLocal is declared, which declares its own local scope variable named $a with a value of 11.

When the function is called, it prints the value of its local scope variable $a, which is 11. This is done by concatenating the string “Variable a from inside the function” with the value of $a using the . operator and printing the result using echo.

After the function is called, the value of the global scope variable $a is printed, which is 9. This is done by concatenating the string “Variable a from outside the function” with the value of $a using the . operator and printing the result using echo.

The final output of the code will be two paragraphs, each containing the value of a different $a variable.


Global Keyword In PHP

Using the global keyword allows you to access a global variable within a function.

You can achieve this by using the global keyword before the variable (inside the function):

PHP Global Keyword Example: 1 

<?php $a = 45; $b = 20; function globalKeyword() { global $a, $b; $b = $a + $b; } globalKeyword(); echo $b; // outputs 65 ?>

PHP Global Keyword Example: 2 

<?php $a = "AliExpress is one of the "; function globalKeyword() { global $a; $b = "biggest online store"; $a = $a . $b; } globalKeyword(); echo $a; // AliExpress is one of the biggest online store ?>

Likewise, PHP stores all global variables in an array named $GLOBALS[index].

Variable names are stored in the index. A global variable can be directly updated from within functions using this array.

Rewriting the above example would be as follows:

PHP Global Keyword Example: 3 

<?php $a = 45; $b = 20; function globalKeyword() { $GLOBALS['b'] = $GLOBALS['a'] + $GLOBALS['b']; } globalKeyword(); echo $b; // outputs 65 ?>

PHP Global Keyword Example: 4 

<?php $a = "AliExpress is one of the "; function globalKeyword() { $b = "biggest online store"; $GLOBALS['a'] = $GLOBALS['a'] . $b; } globalKeyword(); echo $a; // AliExpress is one of the biggest online store ?>

Static Keyword In PHP

As soon as a function has been completed/executed, all its variables are deleted.

In some cases, we don’t want a local variable to be deleted.

We need it later. To perform some further jobs.

To accomplish this, declare the variable with the static keyword:

PHP Static Keyword Example: 1 

<?php function staticKeyword() { static $a = 0; echo $a; $a++; } staticKeyword(); staticKeyword(); staticKeyword(); ?>

PHP Static Keyword Example: 2 

<?php function staticKeyword() { static $a = 3; // static variable $b = 7; $b++; $a++; echo $b, "\n"; echo $a, "\n"; } staticKeyword(); //8 4 staticKeyword(); //8 5 ?>

This means that each time the function is called, the variable will still contain the same information as before.

Remember: Variables within the functions are still local to them.

Knowing how PHP variables scope work and how to use them is essential. When you understand the default scope of variables inside and outside of functions, and when the global keyword is needed, you can write PHP code that works as intended. PHP applications can be developed robustly and reliably when you have this knowledge.

We value your feedback.
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0

Subscribe To Our Newsletter
Enter your email to receive a weekly round-up of our best posts. Learn more!
icon

Leave a Reply

Your email address will not be published. Required fields are marked *