PHP Tutorial

PHP Flow Control

PHP Functions

PHP String

PHP Array

PHP Date Time

PHP Object Oriented

Regular Expression

PHP Cookie & Session

PHP Error & Exception handling

MySQL in PHP

PHP File Directory

PHP Image Processing

PHP Variable Scope

There are three types of variable scopes in PHP:

  1. Local Scope
  2. Global Scope
  3. Static Scope

Let's understand each one with an example.

1. Local Scope

If you declare a variable inside a function, it is a local variable and can only be used inside that function.

Example:

function myTest() {
    $x = 5; // local scope
    echo "<p>Variable x inside function is: $x</p>";
} 
myTest();

// using x outside the function will generate an error
echo "<p>Variable x outside function is: $x</p>";

In the above example, the echo statement outside the function will result in an error because $x is a local variable to the myTest() function and it's not accessible outside of it.

2. Global Scope

A variable declared outside a function has a global scope and can only be accessed outside a function.

Example:

$x = 5; // global scope

function myTest() {
    // using x inside this function will generate an error
    echo "<p>Variable x inside function is: $x</p>";
} 
myTest();

echo "<p>Variable x outside function is: $x</p>";

In the above example, the echo statement inside the function will result in an error because $x is a global variable and is not accessible within the myTest() function.

To access a global variable inside a function, you need to use the global keyword:

$x = 5; // global scope
$y = 10; // global scope

function myTest() {
    global $x, $y;
    $y = $x + $y;
}

myTest();
echo $y; // outputs 15

In the above example, $x and $y are global variables. This is indicated in the myTest function with the global keyword before the variables. Therefore, the function is able to access the global variables and perform the addition operation.

3. Static Scope

If you declare a variable as static inside a function, the variable will remember its value each time the function is called. This is unlike a typical local variable which loses its value when the function's execution is completed.

Example:

function myTest() {
    static $x = 0;
    echo $x;
    $x++;
}

myTest();
myTest();
myTest();

In the above example, the variable $x is declared as static. This means that it retains its value between function calls. So, the first time myTest() is called, it outputs '0'. The second time, it outputs '1', and the third time, it outputs '2'.

That is a brief overview of variable scope in PHP. Understanding variable scope is very important for writing effective PHP code.

  1. Local and global variables in PHP:

    <?php
    $globalVar = "I'm global"; // Global variable
    
    function myFunction() {
        $localVar = "I'm local"; // Local variable
        echo $localVar;
    }
    
    myFunction();
    echo $globalVar;
    
  2. Static variables and scope in PHP:

    <?php
    function counter() {
        static $count = 0;
        $count++;
        echo $count;
    }
    
    counter(); // Outputs: 1
    counter(); // Outputs: 2
    
  3. PHP variable scope within functions:

    <?php
    function myFunction() {
        $functionVar = "Inside function";
        echo $functionVar;
    }
    
    myFunction();
    
  4. PHP variable scope in classes:

    <?php
    class MyClass {
        public $classVar = "I'm a class variable";
    
        public function printVar() {
            echo $this->classVar;
        }
    }
    
    $obj = new MyClass();
    $obj->printVar();
    
  5. Scope resolution operator in PHP:

    <?php
    $globalVar = "I'm global"; // Global variable
    
    function myFunction() {
        global $globalVar;
        echo $globalVar;
    }
    
    myFunction();
    
  6. Dynamic variable scope in PHP:

    <?php
    $scope = "global";
    
    function myFunction() {
        global $scope;
        echo $scope;
    }
    
    myFunction();
    
  7. PHP variable scope and include/require statements:

    <!-- File: config.php -->
    <?php
    $configVar = "Configuration variable";
    
    <!-- File: index.php -->
    <?php
    include "config.php";
    echo $configVar;
    
  8. PHP variable scope in loops:

    <?php
    for ($i = 0; $i < 5; $i++) {
        $loopVar = "Iteration $i";
        echo $loopVar . "<br>";
    }
    
  9. Changing variable scope in PHP:

    <?php
    function changeScope() {
        $variable = "Local variable";
        return $variable;
    }
    
    $globalVariable = changeScope();
    echo $globalVariable;
    
  10. Nested functions and variable scope in PHP:

    <?php
    function outerFunction() {
        $outerVar = "I'm outer";
    
        function innerFunction() {
            global $outerVar;
            echo $outerVar;
        }
    
        innerFunction();
    }
    
    outerFunction();
    
  11. PHP variable scope and closures:

    <?php
    $outerVar = "I'm outer";
    
    $closure = function() use ($outerVar) {
        echo $outerVar;
    };
    
    $closure();