What is the difference between “GLOBAL” and “STATIC” variable in PHP?

前端 未结 4 1759
眼角桃花
眼角桃花 2021-01-02 10:29

What exactly is the difference between the GLOBAL and STATIC variables in PHP? And which one is preferable to use, when we want to use a variable in multiple functions?

相关标签:
4条回答
  • 2021-01-02 10:58

    Global variable is created before the function is created, but global keyword is added in function

    $g_var = 1;
    
    function test() {
      var_dump($GLOBAL['g_var']);
      global $g_var;
      var_dump($g_var);
    } 
    

    static is created and declared static in function

    function test() {
      static $cnt = 0;
      $cnt ++;
      echo $cnt;
    }
    
    $i = 10;
    
    while(--$i) test();
    
    0 讨论(0)
  • 2021-01-02 11:00

    A static variable just implies that the var belongs to a class but can be referenced without having to instantiate said class. A global var lives in the global namespace and can be referenced by any function in any class. Global vars are always frowned upon because they're so easily misused, overwritten, accidentally referenced, etc. At least with static vars you need to reference via Class::var;

    0 讨论(0)
  • 2021-01-02 11:08

    A global variable is a variable which is defined in a scope and can span to included and required scopes. (in simple terms by scope I mean the php file/function/class)

    Here are some examples of how global variables work.

    Example 1: Variable declared in scope and used in function using global keyword

    <?php
    $a = 1;
    
    function add_a() {
        global $a;
        $a++;
    }
    
    add_a();
    
    echo $a;
    

    In the above example we declare variable $a and assign it value 1 in the scope. We then declare a function add_a in the same scope and try to increment the $a variable value. The add_a function is called and then we echo the $a variable expecting the result to display 2.

    Example 2: Variable declared in scope and used in function using the $GLOBALS variable

    <?php
    $a = 1;
    
    function add_a() {
        $GLOBALS['a']++;
    }
    
    add_a();
    
    echo $a;
    

    The result from example 2 above is exactly the same as the result from example 1.

    There is no difference with using the global keyword and the special PHP defined $GLOBALS array variable. However they both have their advantages and disadvantages.

    Read more about $GLOBALS on official PHP website $GLOBALS

    If you want to span a scope declared variable to a included or required scope see example below.

    Example 3: file a.php

    <?php
    global $a;
    $a = 1;
    
    require 'b.php';
    
    add_a();
    
    echo $a;
    

    file b.php

    <?php
    function add_a() {
        global $a;
        $a++;
    }
    

    In the above example we have file a.php and b.php. File b.php is required in file a.php because we use a function declared in file b.php. To use the $a variable in file b.php we must first declare $a as global to be used outside the local scope and we do this by first calling global $a and then we define a value like so $a = 1. Variable $a is now available to be used anywhere in any included scopes by first calling global $a before manipulation.

    Static Variables Usually found in classes but in some well developed PHP project you can find them in recursive functions. A static variable is a variable that remembers its value and can be reused every time the function or method is called.

    Here are some examples of a static variable in use.

    Example 1: Static variable in a function

    function add() {
        static $a = 1;
        $a++;
    
        echo $a;
    }
    
    add(); //2
    add(); //3
    add(); //4
    

    Example 2: Static variable in class

    class A {
        public static $a = 1;
    
        public static function add() {
            self::$a++;
            echo self::$a;
        }
    }
    
    echo A::$a; //1
    A::add(); //2
    
    echo A::$a; //2
    A::add(); //3
    
    echo A::$a; //3
    A::add(); //4
    

    Note that you cannot assign a return value from a function to a static variable. For example you cannot do static $a = rand(). See Example 3 below on how to assign return value to static variable.

    Example 3: Assign return variable from function to static variable

    function add() {
        static $a;
        $a = rand();
    
        echo $a;
    }
    

    Read more about global and static variables on PHP official website Variable scope

    0 讨论(0)
  • 2021-01-02 11:14

    Global is used to get the global vars which may be defined in other scripts, or not in the same scope.

    e.g.

    <?php
    
    $g_var = 1;
    
    function test() {
       var_dump($GLOBAL['g_var']);
    
       global $g_var;
       var_dump($g_var);
    } 
    

    Static is used to define an var which has whole script life, and init only once.

    e.g.

    <?php
    
    function test() {
        static $cnt = 0;
        $cnt ++;
        echo $cnt;
    } 
    
    $i = 10;
    
    while (-- $i) {
        test();
    }
    
    0 讨论(0)
提交回复
热议问题