In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
In this issue, the editor will bring you about how to solve the problem that php global variables can not be used in the function. The article is rich in content and analyzes and describes for you from a professional point of view. I hope you can get something after reading this article.
Solution: 1, use the global keyword to import global variables in the function, syntax "global global variable 1, global variable 2,..."; 2, use the super global variable "$GLOBALS" to access the specified global variable, syntax "$GLOBALS ['global variable name']".
Operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer
In general, global variables cannot be used within a function, but sometimes we have to use global variables within a function. What should we do?
PHP provides two workarounds: the global keyword and the $globals hyperglobal variable.
Method 1. Use the global keyword
Take a look at the output as follows:
$a = 1$ b = 2$ c = 3
Analysis:
The function of the global keyword is to import global variables, using global variable 1, variable 2,. In the form of global variable 1, variable 2. When imported into the local scope of a function, global variables defined outside the function can be used inside the function.
There are a few points to pay attention to when using the global keyword:
The global keyword can only be used inside the function, not outside the function.
The global keyword can only be used to refer to global variables outside the function, and cannot be assigned directly when referencing. Assignment and declaration statements need to be written separately.
When a variable modified with the global keyword is destroyed inside the function, the variables outside the function are not affected.
Method 2: use the $Globals hyperglobal variable
The output is as follows:
$a = 1$ b = 2$ c = 3
Analysis:
GLOBALS is a predefined variable (also known as a hyperglobal variable), which is a global combinatorial array of all variables. The name of the variable is the key of the array, and you can access the specified global variable in the form of $GLOBALS ['variable name']. As long as it is a global variable that has appeared, it can be obtained through the array of $GLOBALS.
The difference between global and $GLOBALS
Global can only be used inside the function, not outside the function, while $GLOBALS can be used anywhere in the program (inside or outside the function).
When a variable decorated with the global keyword is destroyed inside the function, the variables outside the function are not affected; $GLOBALS is affected.
Reason:
When you modify the $var variable with the global keyword, it is a reference to the variable of the same name outside the function, and the inside and outside are two variables that do not affect each other.
And $GLOBALS ['var'] refers to the variable itself outside the function, which is a variable.
The above is the editor for you to share how to solve the problem that php global variables can not be used in the function, if you happen to have similar doubts, you might as well refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.