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

前端 未结 4 1787
眼角桃花
眼角桃花 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 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;

提交回复
热议问题