Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to understand the scope and life cycle of constants and variables in php

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces "how to understand the scope and life cycle of constants, variables in php". In daily operation, I believe many people have doubts about how to understand the scope and life cycle of constants and variables in php. The editor consulted all kinds of data and sorted out simple and useful methods of operation. I hope it will be helpful for you to answer the doubts of "how to understand constants, the scope and life cycle of variables in php". Next, please follow the editor to study!

The main variables in PHP scripts are: built-in super global variables, general variables, constants, global variables, static variables and so on. When we use them, we should not only know their syntax correctly, but more importantly, we need to know the essential difference and relationship between them-that is, their scope.

1. Built-in super global variables can be used and visible anywhere in the script. That is, if we change one of the values in one PHP page, the value will change when we use it in other PHP pages.

two。 Once declared, constants will be visible globally, that is, they can be used inside and outside the function, but this is limited to one page (including the PHP scripts we included through include and include_once), but not on other pages.

3. The global variable declared in a script is visible throughout the script, but not inside the function. If the variable inside the function has the same name as the global variable, the variable inside the function shall prevail.

4. When the variable used inside the function is declared as a global variable, its name should be the same as the name of the global variable. In this case, we can use the global variable outside the function in the function. In this way, we can avoid the previous situation that the external variable is overwritten because the internal variable of the function has the same name as the external global variable.

5. Variables that are created inside the function and declared static are not visible outside the function, but the value can be maintained during multiple execution of the function, most commonly during the recursive execution of the function.

6. The variable created inside the function is local to the function, and when the function terminates, the variable no longer exists.

The complete list of super global variables is as follows:

1.$GOBALS array of all global variables

2.$_SERVER server environment variable array

The array of variables passed to the script by 3.$_POST through the POST method

The array of variables passed to the script by 4.$_GET through the GET method

Array of 5.$_COOKIE cookie variables

6.$_FILES array of variables related to file upload

7.$ENV environment variable array

The array of variables entered by all users of 8.$_REQUEST includes the input contained in $_ GET $_ POST $_ COOKIE

9.$_SESSION session variable array

We should note that another important difference between variables and constants is that constants can only define boolean (Boolean), integer (integer), float (floating point) and string (string) data, but not resource data.

At this point, the study on "how to understand the scope and life cycle of constants, variables in php" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report