Variable scope In PHP

When using variables and functions, consider the scope of variables. The scope defines the scope of the accessibility of this variable.

Variables in Loop and Conditional Blocks

Loops and conditional blocks do not form a separate scope, and we can use the variables defined in these blocks outside of these blocks:

$condition = true;
if($condition){
    
    $name = "Tom";
}
echo $name; // Tom

//or
 
$i = 6;
switch($i){
    
    case 5: $name = "Tom"; break;
    case 6: $name = "Bob"; break;
    default: $name = "Sam"; break;
}
echo $name; // Bob

Local variables

Local variables are created inside the function. Such variables can only be accessed from inside the given function. For example:

<?php
function showName(){
    $name = "Tom";
    echo $name;
}

showName();
echo $name; // you can't write like this, because
            // variable $name exists
            // only inside showName(
?>

In this case, showName() a local variable is defined in the function $name. Accordingly, we can refer to this variable only inside the function showName(), but not outside it.

The same applies to function parameters: outside the function, its parameters also do not exist.

Static variables

Static variables are similar to local variables. They differ in that after the function ends, their value is saved. With each new call, the function uses the previously stored value. For example:

<?php
function getCounter()
{
    static $counter = 0;
    $counter++;
    echo $counter;
}
getCounter(); // counter=1
getCounter(); // counter=2
getCounter(); // counter=3
?>

To indicate that a variable will be static, the static keyword is added to it . On three consecutive function calls, the getCounter()$counter variable will be incremented by one.

If the $counter variable were a normal non-static variable, then the getCounter() function would output 1 each time it was called.

As a rule, static variables are used to create various counters, as in the example above.

Global variables

Global variables are not accessible inside a function by default. For example:

<?php
$name = "Tom";
function hello()
{
    echo "Hello " . $name;
}
hello();
?>

This code will not work, and the PHP interpreter will notify us that the variable $nameis not defined.

However, we can access a global variable within a function. To do this, you need to use the global keyword :

<?php
$name = "Tom";
function hello()
{
    global $name;
    echo "Hello " . $name;
}
hello();    // Hello Tom
?>

To access a global variable in a function, a variable with the same name is declared using the global statement:

global $name;

After that, the global variable $name can be accessed inside the function. Moreover, we can not only get its value, but also change it:

 
<?php
$name = "Tom";
function changeName()
{
    global $name;
    $name = "Tomas";
}
changeName();   
echo $name; // Tomas
?>

As an alternative to the global statement, we can use the built-in $GLOBALS array to access global variables :

<?php
$name = "Tom";
function changeName()
{
    $username = $GLOBALS["name"];
    echo "Old name: $username <br>";
    // change the value of the $name variable
    $GLOBALS["name"] = "Tomas";
}
changeName();   
echo "New name: " . $name;
?>

To refer to the global variable $name, an expression $GLOBALS[“name”]is used – the name of the variable (without the $ sign) is passed in square brackets. The result of the script:

Old name: Tom
New name: Thomas