Yogesh Chauhan's Blog

PHP Variables Scope

in PHP on 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>";
?>

Most Read

#1 How to check if radio button is checked or not using JavaScript? #2 Solution to “TypeError: ‘x’ is not iterable” in Angular 9 #3 How to add Read More Read Less Button using JavaScript? #4 How to uninstall Cocoapods from the Mac OS? #5 PHP Login System using PDO Part 1: Create User Registration Page #6 How to Use SQL MAX() Function with Dates?

Recently Posted

#Aug 15 Is PHP still good for back-end programming? #Aug 10 How to create a multisite network in WordPress? #Aug 3 How to create a circle that follows a cursor using JavaScript and CSS? #Aug 3 How to make a curtain slider using jQuery and CSS? #Aug 2 How to progressively load images and add a blurry placeholder? #Aug 1 How to create a placeholder loader (throbber) using CSS?
You might also like these
CROSS JOIN in PostgresPostgresQuery to increment or decrement value in MySQL ignoring negative valuesSQL/MySQL3 Common Usability Mistakes In Web DesignUI/UXHow to switch dark and light themes using pure CSS?CSSHow to convert a number rounding to a specified number of decimals in JavaScript?JavaScriptWhat’s new in Constructor in PHP 8?PHP