Global Variables vs. Local Variables

后端 未结 6 993
忘了有多久
忘了有多久 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:37

    Local variables are needed for encapsulation, which is a good practice in programming.

    Global variables in fact is a bad practice, because a variable can be altered anywhere in the application, and believe me, is hard to debug why the value of a variable is not the expected if you have multiples includes with multiples includes...

    If you need to use a global variable, perhaps you should consider using a singleton or pass the variable as a parameter to the functions that need it.

自定义标题
段落格式
字体
字号
代码语言
提交回复
热议问题