Home  >  Article  >  Global variables cannot be defined in

Global variables cannot be defined in

(*-*)浩
(*-*)浩Original
2019-07-27 09:19:4213747browse

Global variables are a type of programming terminology, derived from variables.

Global variables cannot be defined in

#Variables are divided into local and global variables. Local variables can also be called internal variables. Variables created by an object or a function are usually local variables and can only be referenced internally and cannot be referenced by other objects or functions. (Recommended learning: PHP video tutorial)

The definition of global variables cannot be inside the function, because the variables created inside the function are called local variables. Global variables can be created by an object function or anywhere in the program. Global variables can be referenced by all objects or functions in this program.

The existence of global variables is mainly due to the following reasons:

1. Using global variables will occupy more memory (because of its long life span), However, today with high computer configurations, this should not be a problem, unless you are using global variables of huge objects, you must avoid it if you can.

2. When using global variables, the program runs faster (because memory does not need to be reallocated), but it is not much faster either.

3. Regarding namespace pollution of local variables, this can be avoided if too many variables are not used.

4. When the global variable and the local variable have the same name, the local variable takes effect and the global variable is blocked.

5, you can also use extern to declare global variables outside the function, so that the scope of the global variable ranges from the declaration point to the end of the file.

6, global variables have lower priority than local variables.

In short, global variables can be used, but when using global variables, attention should be paid to making their names as easy to understand as possible and not too short to avoid name space pollution; avoid using global variables of huge objects.

In modern object-oriented languages ​​such as Java, C, C#, and Ruby, because variables are encapsulated in classes and invisible to other classes, the concept of global variables has been almost completely abandoned.

However, you can realize the function of global variables by defining a class as public static and defining the class member variables as public static so that the variable occupies a fixed and unique space in the memory.

For more PHP related technical articles, please visit the PHP Graphic Tutorial column to learn!

The above is the detailed content of Global variables cannot be defined in. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn