Global and Local Scope

Total Post:113

Points:791
PHP
 490  View(s)
Ratings:
Rate this:

Difference between Global and Local Scope variables in PHP ?

  1. Post:140

    Points:984
    Re: Global and Local Scope

    Global Scope:

    Global scope is a variable which declared outside a function and can only be accessed outside a function. 

    Example :

    <?php
    
    $x = 5; // global scope
    function mindstick() {
        echo "<p>Variable x inside function is: $x</p>";
    }
    mindstick();
    echo "<p>Variable x outside function is: $x</p>";
    ?>


    Local Scope:

    A variable declared within a function has a LOCAL SCOPE and can only be accessed within that function. 

    Example :

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

Answer

Please check, If you want to make this post sponsored

You are not a Sponsored Member. Click Here to Subscribe the Membership.