PHP

Scope Of Variables In PHP

In this tutorial, we are going to see what are variables and the Scope of variables in PHP but first let’s understand the concept of variables in PHP, starting with the introduction.

What is a Variable in PHP?

A variable in PHP is used to store some temporary data, There are three scopes of variables we can use to store values.

The first one is “local” the second is “global” and the third one is “static”. Let’s understand the scopes of variables in depth.

Local Variables in PHP

It is a type of variable that can only be used inside the function a local variable is not accessible outside the function.

See the example below.

Example


<?php 
$int = 10;
function index()
{  
    $int = 5;
    echo "variable inside the function is = $int \n";
}
index();
echo "the Variable outside the function is $int \n"; 
?>

Output

The variable inside the function is = 5 the Variable outside the function is 10.

As you can in the output the value of a variable inside the function did not print outside the function.

Let’s move forward to the second scope of the variable.

Global Variables in PHP

These are the variables that can be accessed outside and inside of the function as well that’s why they are called global variables. See the example below.

Example

<?php

$int = 10;

function index()
{  
    global $int;
    echo "varible inside the function is = $int \n";
} 
index();
echo "the Variable outside the function is $int \n";
?>

Output

The variable inside the function is = 10 the Variable outside the function is 10.

we took the same example as above just change the variable local to global and as you can see it is showing the same variable inside and outside the function.

Static Variables in PHP

These are the variables that are used to store that data which can be used even after the completion of the function, means to say a static variable can keep the data even if the task has been completed.

Let’s understand this with an example

Example

<?php

function sta_vari()
{  
    // declaring a static and local variable
    static $no = 10;
    $add = 5;
     
    $add++;
    $no++;
     
    echo $no, "\n";
    echo $add, "\n";
}

// calling function
sta_vari();

// calling function for other variable
sta_vari();

?>

Code Explanation

In the above code, we created a function sta_vari(), and then we declared a static variable and a local variable, After that we incremented both variables.

In the end, we just printed both variables and called the function twice.

Let’s check the results.

Output

11 6 12 6

As you can see from the output above the static variable has incremented after calling the function.

But the local variable did not increment because the value in it has freed after the first function call.

Conclusion

In this tutorial, we talked about the Scope of variables in PHP. First of all, we discussed a brief introduction to variables, and then we discussed the scope of variables in PHP.

We saw deeply how the local, and global variables work with examples, and in the end, we also learned about static variables and why we use it, we saw their examples as well.

That’s all for today hope you like the tutorial if you have any queries regarding this tutorial so feel free to contact us below. Thanks for reading.

Leave a Comment