In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly shows you "what are the differences between global variables and local variables in php", the content is simple and clear, and I hope it can help you solve your doubts. Let me lead you to study and learn about "what are the differences between global variables and local variables in php".
Differences: 1. A local variable is a variable defined within a function and can only be used within the function in which it is defined, while a global variable is a variable defined outside all functions and its scope is anywhere in the current source code. but it's not available inside the function. 2. Local variables are automatically destroyed at the end of the function call, while global variables are destroyed at the end of the program.
Operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer
The scope of a variable (the scope that can be used) is called a variable scope, and a variable must be used within its valid scope, and if it exceeds the valid range, the variable loses its meaning. PHP variables can be divided into global variables and local variables according to scope.
Variables that can be used anywhere in the current source code (outside the function) are called global variables, and they have global scope; variables that can only be used within the function that defines them are called local variables, and they have local scope.
In PHP, global variables cannot be used directly inside the function, and local variables cannot be used directly outside the function. Of course, this is not absolute, through some methods it is possible to call global variables within the function and local variables outside the function, which we will introduce in later study.
Local variables and local scope
A local variable is a variable defined inside a function and can only be used within the function in which it is defined. Local variables are automatically destroyed at the end of the function call.
[example] the following defines a function named example, defines a local variable an inside the function, and then attempts to output the value of variable an inside and outside the function. The specific code is as follows:
The running results are as follows:
From the running results, we can see that the local variable defined inside the function cannot be called outside the function, because the scope of the local variable $an is the example () function that defines it and cannot be used outside the function.
Global variables and global scope
A global variable is a variable defined outside all functions, whose scope is anywhere in the current source code, but is not available within the function. Global variables always exist while the program is running, and will be destroyed only when the program is finished running.
[example] define a global variable a, and output the value of the global variable inside and outside the function respectively. The specific code is as follows:
The running results are as follows:
As you can see from the running results, $an is not successfully called inside the function, but it can be done outside the function.
These are all the contents of this article entitled "what are the differences between global variables and local variables in php". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, 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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.