YogeshChauhan.com
PHP Variables Scope
November 8, 2019


In PHP, you can declare variables anywhere in the script.

Variable Scope: Part of the script where variable can be used or allowed to use. If you try to use variables which are not allowed to use in the scope then you will get an error.

There are 3 variable scopes in PHP:

  • Local
  • Global
  • Static

Let's look at those variables one by one. Starting form global:

We declare global variables outside the functions and we can not access them from inside the function.

For example, look at this code:


<?php
$x = "global"; // this variable $x has a global scope

function scope() {
    // If we try to use $x inside this function, it will generate an error
    echo "<p>I am $x Variable inside the function (I am missing the word global as I can't access the variable)</p>";
}
scope();

echo "<p>I am $x Variable outside the function</p> ";
?>

Take a look at the output in the DEMO link given at the end of this article.

Second one is local variable which you can figure out by name that it's scope will be limited to local function only.

It's because when the function is fully executed, the variables inside that function are deleted. 

For example, look at this code:


<?php

function scope2() {
    $y = "local"; // this variable $x has a local scope
    // If we try to use $x inside this function, it will generate an error
    echo "<p>I am $y Variable inside the function</p>";
}
scope2();

echo "<p>I am $y Variable outside the function (I am missing the word local as I can't access the variable)</p> ";
?>

Take a look at the output in the DEMO link given at the end of this article.

Third one is static variable. 

I just mentioned in the local variable that when the function is fully executed, the variables inside that function are deleted. But what if we want to save that variable and stop getting deleted?

We need to use static keywords while declaring those variables if we want to save them. However the scope of the variables will be limited ot the function only.

Take a look at this example.


<?php
function statVar() {
    static $z = 0;
    echo $z;
    $z++;
}

statVar(); echo "<br>";
statVar(); echo "<br>";
statVar();

echo "<p>Value of z doesn't stay at 0 everytime the function is called</p>";
?>
dreamhost

Leave a Reply

Most Read

#1 How to check if radio button is checked or not using JavaScript? #2 How to set opacity or transparency using CSS? #3 Pagination in CSS with multiple examples #4 How to make HTML form interactive and using CSS? #5 How to uninstall Cocoapods from the Mac OS? #6 Solution to “TypeError: ‘x’ is not iterable” in Angular 9



Recently Posted

Feb 25 How to set default timezone using PHP? Feb 24 WordPress: How to loop through a repeater field in ACF? Feb 24 How to get ACF values from custom post type? Feb 24 WordPress get_posts Examples Feb 24 How to modify the latest post array using get_posts() in WordPress? Feb 22 WordPress: How to get ACF field values from another post?



You might also like these

What is XP (Extreme Programming) and When should it be used?MiscHow to Create a Copy of a Table in SQL and MySQL?SQL/MySQLHow Do You Make a Private VPN?MiscOverview of Drupal and how it worksDrupalHAVING Clause in Postgres with Examples in All Aggregate FunctionsPostgresUnary Operators in JavaScriptJavaScript