PHP variable types: local, global or superglobal

In PHP, as in other programming languages ​​such as Javascript, the place where we create our variables or the way to do it will define their availability.

Content

  • What is the scope of a php variable?
  • Local scope variables in php
  • Static variables in php
  • Global scope variables in php
    • The GLOBAL PHP keyword
    • The $GLOBALS variable
  • PHP superglobal scope variables
  • Extra: Class local variables or Class Attributes

What is the scope of a php variable?

The scope of a variable could be defined as its degree of availability throughout our programming. This scope in PHP can be, mainly, of 4 types:

  • Local
  • Global
  • Static
  • super global

In PHP we could define the scope of a variable as its degree of availability throughout our programming.

Local scope variables in php

Local scope variables are those that we define inside a function, either an individual function or belonging to a function. php class. The fact that these variables are local means that they will not exist outside the function, since they will be destroyed when the function code finishes executing. Also, if we had a variable created outside the function with the same name, these two variables would be completely different.

An example could be the following:

<?php
$var1 = 3;
function mostrar() {
    $var1 = 2;
    $var1 = 1;
    echo $var1 . '<br/>';
}
echo $var1 . '<br/>';
mostrar();
?>

The above code demonstrates the scope of the local variable created inside the function. So if we ran this little php-script the screen output would be:

3
1

Local variables are destroyed after the function is executed, that is, when we call the function again, it is redefined with the first value assigned to it, regardless of previous calls. So continuous calls to the function from the previous example:

<?php
mostrar();
mostrar();
mostrar();
?>

They would show the following output:

1
1
1

Static variables in php

Static variables are defined inside a function with the STATIC keyword in front of it.

Static variables are a special type of local variable, like an extension of function local variables but persist after the execution of the containing function. The fact that the static variable persists means that its last value will be the value of the variable when we call the function again.

Let’s see the following example:

<?php
function incrementar(){
   STATIC $estatica1 = 1;
   $estatica++;
   echo $estatica. '<br/>';   
}
incrementar();
incrementar();
incrementar();
?>

These consecutive calls will cause the variable $static1 to be incremented on each call, since static variables are only defined with the first call to the function that defines them. The output would be:

2
3
4

Global scope variables in php

Unlike local scope variables, global scope variables, also called globals, are accessible anywhere in our application.

Creating a global variable is as easy as defining it anywhere in our application other than:

  • A function, either as an input parameter or not
  • A method of a php class
  • As a parameter of a class

So in this example you would be creating a global variable:

<?php
$miVarGlobal = 1;
?>

Now that we know how to create global variables, we must point out one piece of information, and that is that inside functions or class methods, these variables are invisible by default. So if we review the example from the previous point again:

<?php
$var1 = 3;
function mostrar() {
    $var1 = 2;
    $var1 = 1;
    echo $var1 . '<br/>';
}
echo $var1 . '<br/>';
mostrar();
?>

As you can see there is a variable $var1 that is defined first as global, and inside the function as local. How could we have created this variable locally and globally at the same time? Well, you will see, as I was saying, global variables, by default, are invisible to functions.

The GLOBAL PHP keyword

If we wanted the function to have access to the global variable, we would specify it with the GLOBAL keyword:

<?php
$var1 = 3;
function mostrar() {
    GLOBAL $var1;
    $var1 = 1;
    echo $var1 . '<br/>';
}
echo $var1 . '<br/>';
mostrar();
echo $var1 . '<br/>';
?>

So in this example the screen output would be the following:

3
1
1

The last two lines explain the new behavior of the variable inside the function. Because I’ve written the GLOBAL keyword in front of the name, I’ve indicated that I want to use the available global variable instead of defining a new one.

The $GLOBALS variable

To access the global variables of our development, apart from using the keyword GLOBAL (it can also be written in lower case), we can also use the array$GLOBALindicating in the clue of the array the name of the variable. The following example illustrates this case:

<?php
$var1 = 3;
function mostrar() {
    $GLOBALS['var1'] = 1;
    echo $GLOBALS['var1'] . '<br/>';
}
echo $var1 . '<br/>';
mostrar();
echo $var1 . '<br/>';
?>

The output of this example would be exactly the same as the previous one:

3
1
1

PHP superglobal scope variables

The variables with the greatest availability are those of scope super global, why are they called super global? Well, because unlike the global ones, these variables are accessible at any point in our programming and it is also not necessary to use the GLOBAL keyword inside the functions or methods.

scope variables super global are for example:

  • The form information arrays $_GET, $_POST, $_REQUEST, $_FILES
  • Server Information Variables $_SERVER
  • Array of global variables $GLOBALS
  • Session variable and cookies $_SESSION and $_COOKIE
  • Constants Defined Using the define() Function

These variables are accessible at any point in our programming and it is not necessary to use the GLOBAL keyword inside the functions or methods.

Extra: Class local variables or Class Attributes

When we work with php in Object Oriented Programming (OOP) a class of scope variables appears somewhat different from the local variables of functions. These variables, or attributes (as they are called), are accessible throughout the code of the class, so they are shared among all the methods (functions of the object).

Class attributes are only accessible by prefixing the $this-> keyword in any of its methods.

Leave a Reply