Home > Backend Development > PHP Tutorial > Magic constants, predefined constants and predefined variables in PHP

Magic constants, predefined constants and predefined variables in PHP

怪我咯
Release: 2023-03-10 20:14:02
Original
1739 people have browsed it

1. Magic constants

There are eight magic constants in PHP, and their values ​​will change as their positions in the code change. These special constants are not case-sensitive.

  • LINE: Returns the current line number in the file. It can also be written as line.

  • FILE: Returns the absolute path of the current file (including the file name).

  • DIR: Returns the absolute path of the current file (excluding the file name), equivalent to dirname(FILE).

  • FUNCTION: Returns the name of the current function (or method).

  • CLASS: Returns the current class name (including the scope or namespace of the class).

  • TRAIT: Returns the current trait name (including the trait's scope or namespace).

  • METHOD: Returns the current method name (including class name).

  • NAMESPACE: Returns the name of the current file's namespace.

2. Predefined constants

Kernel predefined constants: They are constants defined in the PHP kernel. case sensitive.

PHP_VERSION: Returns the PHP version.

PHP_OS: Returns the name of the operating system executing the PHP interpreter.

PHP_EOL: System newline character, Windows is (\r\n), Linux is (\n), MAC is (\r).


Standard predefined constants: constants defined by PHP by default. case sensitive.

M_PI: Returns the value of pi.

3. Predefined variables

Many predefined variables in php are "superglobal", which means that they are in the entire scope of a script are available. They can be accessed within a function or method without executing global $variable.

Superglobal variables are built-in variables that are always available in all scopes.

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

  • $_GET: HTTP GET variables, an array of variables passed to the current script via URL parameters.

  • $_POST: HTTP POST variable, an array of variables passed to the current script through HTTP POST.

  • $_COOKIE: HTTP Cookies variable, an array of variables passed to the current script through HTTP Cookies.

  • #$_SESSION: session variable, an array of SESSION variables available to the current script.

  • $_REQUEST: HTTP Request variable, by default contains an array of $_GET, $_POST and $_COOKIE.

  • $_FILES: HTTP File upload variable, an array of items uploaded to the current script through HTTP POST.

  • $_SERVER: Server information variable, containing an array of information such as header, path, and script locations. The items in this array are created by the web server.

  • #$_ENV: Environment variable, an array of variables passed to the current script through the environment.

The above predefined variables are all super global variables.

The following predefined variables are non-global.

$php_errormsg: The previous error message, the $php_errormsg variable contains the latest error message generated by PHP. This variable is only available in the scope where the error occurred, and requires that the track_errors configuration item is turned on (the default is turned off).


$HTTP_RAW_POST_DATA: Contains the raw data submitted by POST.

$http_response_header: HTTP response header, the $http_response_header array is similar to the get_headers() function. When using an HTTP wrapper, $http_response_header will be populated with HTTP response headers.

$argc: The number of parameters passed to the script, including the number of parameters passed to the current script when running on the command line. The file name of the script is always passed as an argument to the current script, so the minimum value of $argc is 1. This variable is only available when register_argc_argv is turned on.

$argv: Array of parameters passed to the script, containing an array of parameters passed to the current script when running from the command line. The first parameter is always the file name of the current script, so $argv[0] is the script file name. This variable is only available when register_argc_argv is open.

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

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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template