PHP Basics
Functions in PHP
Working with Forms
Working with Files
Working with Databases
Advanced PHP Techniques

In PHP, understanding the concepts of global and local scope is crucial for managing variables and functions in a script. Here’s a detailed explanation:

Local Scope

Local scope refers to variables that are defined within a function. These variables are only accessible within the function itself. They are not accessible from outside the function or within other functions.

Example:

				
					<?php
function myFunction() {
    $localVariable = "I am local";
    echo $localVariable; // This will work
}

myFunction();

echo $localVariable; // This will cause an error
?>

				
			

In this example, ‘$localVariable‘ is defined inside ‘myFunction‘ and can only be used within ‘myFunction‘. Trying to access it outside the function will result in an error.

Global Scope

Global scope refers to variables that are defined outside of any function. These variables are accessible from anywhere in the script, including within functions, but with some restrictions.

Example:

				
					<?php
$globalVariable = "I am global";

function myFunction() {
    global $globalVariable;
    echo $globalVariable; // This will work because of the global keyword
}

myFunction();

echo $globalVariable; // This will also work
?>

				
			

In this example, ‘$globalVariable‘ is defined outside of any function, so it is in the global scope. To use it within a function, you need to declare it as global using the ‘global‘ keyword.

The global Keyword

To access a global variable within a function, you must use the global keyword. This keyword imports the global variable into the local scope of the function.

Example:

				
					<?php
$globalVariable = "I am global";

function myFunction() {
    global $globalVariable;
    $globalVariable = "I have been changed";
}

myFunction();

echo $globalVariable; // Outputs: I have been changed
?>

				
			

The $GLOBALS Array

Another way to access global variables from within a function is to use the’ $GLOBALS‘ array. This is a superglobal associative array in PHP that contains all global variables.

Example:

				
					<?php
$globalVariable = "I am global";

function myFunction() {
    $GLOBALS['globalVariable'] = "I have been changed";
}

myFunction();

echo $globalVariable; // Outputs: I have been changed
?>

				
			

Static Variables

Static variables in PHP retain their value between function calls. They have a local scope within the function but do not lose their value when the function exits.

Example:

				
					<?php
function myFunction() {
    static $count = 0;
    $count++;
    echo $count;
}

myFunction(); // Outputs: 1
myFunction(); // Outputs: 2
myFunction(); // Outputs: 3
?>

				
			

In this example, the static variable $count retains its value across multiple calls to myFunction.

  • Local Scope: Variables declared within a function are local and cannot be accessed outside of that function.
  • Global Scope: Variables declared outside of any function are global and can be accessed anywhere in the script, but require the global keyword or $GLOBALS array to be accessed within functions.
  • Static Variables: Variables declared with the static keyword within a function retain their value between function calls.

Understanding these concepts helps in managing variable scope effectively and avoiding common pitfalls such as variable conflicts and unintended behavior in PHP scripts.

Scroll to Top