Please wait

Variable Scope

In PHP, the scope of a variable refers to the context or part of the code within which the variable can be accessed or manipulated. There are mainly two types of variable scope in PHP:

  • Local Scope: If a variable is defined within a function, it can only be used within that function. This is known as a local variable.
  • Global Scope: If a variable is defined outside of a function, it has a global scope, which means it can be accessed anywhere in the script outside of functions.

Scope examples

Let's look at some examples of scope behavior.

In this example, we have a function called testFunction() with a variable declaration called $localVar. This variable gets echoed onto the page. After echoing the variable, the $globalVar variable gets echoed.

However, we'll receive an error because functions don't have access to variables defined outside of the function.

$globalVar = "I'm a global variable";  // This variable is global
 
function testFunction() {
  $localVar = "I'm a local variable";  // This variable is local to testFunction
  echo $localVar;
 
  echo $globalVar; // throws an error
}
 
testFunction();  // This will print "I'm a local variableI'm a global variable"

Global variables

But what if we want to access variables from the global scope from within a function? PHP offers a keyword called global. After this keyword, you can provide a comma-separated list of variables. In this example, before echoing the $globalVar variable, we're grabbing it from the global scope with the global keyword.

$globalVar = "I'm a global variable";  // This variable is global
 
function testFunction() {
  $localVar = "I'm a local variable";  // This variable is local to testFunction
  echo $localVar;
 
  global $globalVar;  // This is how you access a global variable inside a function
  echo $globalVar;
}
 
testFunction();  // This will print "I'm a local variableI'm a global variable"

Accessing local variables

What if you want to access a variable defined inside a function from outside a function? Variables defined in a function and the parameters of a function are destroyed after a function has finished executing. This is why you can't access them outside of the function.

In that case, you would return the variable. Unlike accessing global variables, you're limited to returning one value.

If you're interested in returning multiple values from a function, you can return an array.

$globalVar = "I'm a global variable";
 
function testFunction() {
  $localVar = "I'm a local variable";
 
  return $localVar;
}
 
$message = testFunction();
 
echo $message;  // I'm a local variable"

Here's an analogy that may help you understand this concept:

Think of a PHP script as a large house (the global scope), and a function as a room in that house (the local scope). A variable defined in the global scope is like a piece of furniture in the main hall of the house: anyone in the house can see it. On the other hand, a variable defined in a function is like a piece of furniture in one specific room: you can only see it and use it when you're in that room. If you're in a different room (or a different function), you can't see or use it.

This concept of scope is fundamental to understanding how variables work in many programming languages, not just PHP. It's important for managing data and controlling what parts of your code can read or modify a variable.

Static variables

**A static variable in PHP is a variable that retains its value even after the function in which it is declared has finished executing. **Normally, when a function finishes executing, all of its local variables are destroyed. But if a variable is declared as static, it is not destroyed and can still hold information if the function is called again.

We can declare a variable as static by adding the static keyword before the variable declaration. Here is a simple example to illustrate:

function countFuncCalls() {
  static $count = 0;
  $count++;
  echo $count;
}
 
countFuncCalls();  // Outputs "1"
countFuncCalls();  // Outputs "2"
countFuncCalls();  // Outputs "3"

In this example, $count is a static variable. The first time countFuncCalls() is called, $count is initialized to 0. Each time the function is called, $count is incremented by 1, and its value is outputted. Even though $count is a local variable within countFuncCalls(), it is not destroyed when countFuncCalls() finishes executing, so it can remember how many times countFuncCalls() has been called.

To provide an analogy, imagine you're reading a book and using a bookmark to remember where you left off. The bookmark here is the static variable. Every time you stop reading (which is like the function finishing execution), you put the bookmark on your current page. When you come back to continue reading (which is like the function being called again), you can pick up right where you left off because the bookmark (the static variable) has remembered your place. Without the bookmark, you would always start back at the beginning of the book each time you came back to it (which is like a regular local variable that forgets its value when the function finishes executing).

Key takeaways

  • A variable with local scope is defined within a function. This variable can only be accessed and modified within the function where it is defined. When the function finishes executing, the variable is destroyed.
  • A variable with global scope is defined outside of all functions. This variable can be accessed and modified anywhere in the script outside of functions.
  • To use a global variable inside a function, it must be declared inside the function with the global keyword.
  • Static variables are defined within a function, just like local variables. Unlike local variables, static variables retain their value between function calls.
  • Static variables are only initialized once, the first time the function is called. On subsequent calls to the function, the static variable retains the value from the previous call.

Comments

Please read this before commenting