PHP predefined: understanding predefined constants and superglobal variables

PHPz
Release: 2023-04-11 12:44:02
Original
1772 people have browsed it

PHP is a dynamic language, and the code written by developers will execute the corresponding logic. But in the process of executing code, we often need something like environment variables or system constants to assist us in completing some tasks. PHP provides some predefined (or built-in) variables that allow developers to quickly obtain and process some important information. These variables are divided into two types: predefined constants and superglobal variables.

Predefined constants

Predefined constants have a special definition in PHP: they are defined before the PHP script starts running. Commonly used predefined constants include:

  • LINE: represents the number of lines of code.

  • #FILE: Represents the path and name of the file.

  • DIR: represents the absolute path of the directory where the file is located.

  • FUNCTION: Represents the function name.

  • CLASS: Represents the class name, including the namespace.

  • TRAIT: Represents the trait name, including the namespace.

  • METHOD: Represents the method name of the class.

  • NAMESPACE: Represents the current namespace name.

These predefined constants are readable, but cannot be reassigned, for example:

Copy after login

The purpose of predefined constants is to help developers quickly locate and debug code, It may be used in error reports, or to record code execution paths and timestamps, etc.

Super global variables

Super global variables are variables that can be accessed anywhere in the PHP script. There is no need to worry about scope or functions. Parameter passing. All superglobal variables are arrays of global variables starting with an underscore, and the array names are different in different versions. There are the following types of superglobal variables:

  • $GLOBALS: A global scope array containing all variables. The name of the variable is the key of the array.

  • $_SERVER: Server and execution environment information. For example: $_SERVER['PHP_SELF'] returns the file name of the currently executing script.

  • $_GET: HTTP GET variable.

  • $_POST: HTTP POST variable.

  • $_FILES: HTTP file upload variable.

  • $_REQUEST: HTTP Request variable, which also contains the contents of $_GET, $_POST and $_COOKIE variables.

  • $_SESSION: Session variable.

  • $_COOKIE: HTTP Cookies.

  • $_ENV: Environment variable.

$_SERVER Example of super global variable:

Copy after login

The advantage of super global variables is that when you need to access global variables, you do not need to pass them in every function these variables. However, because these variables can be directly accessed anywhere in any script, it also brings some security issues. Attackers can use some vulnerabilities to modify the contents of these global variables, causing the system to generate exceptions, and debugging and analyzing the reasons for the exceptions will also more difficult.

Conclusion:

Predefined constants and superglobal variables are some of the variables built into PHP. Their existence makes the development of PHP programmers and debugging become more convenient and efficient. However, when using them, you also need to consider security issues and specific implementation details to avoid unexpected problems.

The above is the detailed content of PHP predefined: understanding predefined constants and superglobal variables. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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