Exploring Variable Scope in PHP

Tutorial 3 of 5

1. Introduction

Goal of the tutorial

This tutorial aims to explore the concept of variable scope in PHP. Variable scope refers to the parts of a script where a variable can be referenced or used. Understanding variable scope is crucial in avoiding programming errors and writing maintainable, high-quality code.

What the user will learn

By the end of this tutorial, you will understand the different scopes of a variable in PHP: global, local, and static. You'll learn where and how these variables can be accessed within your PHP scripts.

Prerequisites

To follow along with this tutorial, you should have a basic understanding of PHP, including how to declare variables and write functions.

2. Step-by-Step Guide

In PHP, variables can be declared anywhere in the script. The scope of a variable is the part of the script where the variable can be accessed. There are three types of scopes: global, local, and static.

Global Scope

A variable declared outside a function has a GLOBAL SCOPE and can only be accessed outside a function.

$x = 5; // global scope

function myTest() {
   // cannot access x inside this function, it is in the global scope
}

myTest();
echo $x; // outputs: 5

Local Scope

A variable declared within a function has a LOCAL SCOPE and can only be accessed within that function.

function myTest() {
   $x = 5; // local scope
   echo $x; 
}

myTest(); // outputs: 5

// cannot access x outside the function, it is in the local scope
echo $x; // gives an error

Static Scope

Normally, when a function is completed/executed, all of its variables are deleted. However, if we declare a variable as static, the variable will not be deleted and its information will be available the next time the function is called.

function myTest() {
   static $x = 0;
   echo $x;
   $x++;
}

myTest(); // outputs: 0
myTest(); // outputs: 1
myTest(); // outputs: 2

3. Code Examples

Example 1: Global Scope

$g = "I'm a global variable!";

function test() {
    // This will output an error since $g is not accessible inside this function
    echo $g;
}

test(); // Gives an error
echo $g; // Outputs: I'm a global variable!

Example 2: Local Scope

function test() {
    $l = "I'm a local variable!";
    echo $l;
}

test(); // Outputs: I'm a local variable!
// This will output an error since $l is not accessible outside the function
echo $l; // Gives an error

Example 3: Static Scope

function test() {
    static $s = 0;
    echo $s;
    $s++;
}

test(); // Outputs: 0
test(); // Outputs: 1
test(); // Outputs: 2

4. Summary

In this tutorial, we discussed the concept of variable scope in PHP and how it impacts the accessibility of variables within your scripts. We explored the three types of scopes: global, local, and static. Understanding variable scope can help you avoid errors and write more readable and maintainable code.

5. Practice Exercises

  1. Create a PHP script with a global variable and try to use it inside a function. What happens?

  2. Write a function with a local variable and try to use it outside the function. What happens?

  3. Write a function with a static variable, call the function multiple times and observe the output.

Solutions

  1. You will get an error because a global variable cannot be accessed inside a function.

  2. You will get an error because a local variable cannot be accessed outside of the function it was declared in.

  3. Each time you call the function, the static variable will retain its previous value. This is different from a normal local variable which gets reset each time the function is called.

Keep practicing with different examples to solidify your understanding of variable scope in PHP. Happy coding!