Global Variables vs. Local Variables

后端 未结 6 990
忘了有多久
忘了有多久 2021-01-12 21:36

Okay, I\'m not sure I\'m understanding this concept properly (I\'m learning PHP). If I understand correctly: Global variables can be referenced anywhere in the same document

6条回答
  •  情话喂你
    2021-01-12 22:32

    In programming, there are many cases where you need a variable only in an isolated context. Counters, function arguments and variables for storing interim results of calculations and such, those aren't of any interest in the whole application. Furthermore, there's the special case of recursive functions. Local variables will get their own distinct addresses for each instance of the recursive function, which is a necessity for recursion to work properly. In PHP applications, however, it's advisable to avoid using global variables wherever you can.

提交回复
热议问题