Home > Article > Backend Development > Detailed explanation of PHP predefined constant examples
What is the role of PHP predefined constants?
We can use PHP's predefined constants to obtain information in PHP. For example, we can use it to obtain the operating system of the current computer, the currently used PHP version information, etc.
Some predefined constants commonly used in PHP are as follows:
Constant name | Function |
__FILE__ | Default constant, PHP program file name |
__LINE__ | Default constant, PHP program line number |
PHP_VERSION | Built-in constants, the version of the PHP program, such as 3.0.8_dev |
Built-in constants, perform PHP parsing The operating system name of the device, such as Windows | |
This constant is a true value (true) | |
This constant is a false value (false) | |
A null value | |
This constant points to the nearest error | |
This constant points to the nearest warning | |
This constant points to potential problems with parsing grammar | |
This constant is a prompt for unusual occurrences but not necessarily Is the error |
Note: The "__" in __FILE__ and __LINE__ in the above table are two underscores, not one "_" .
Example
The following example uses predefined constants to output information in PHP. The code is as follows<?php header("content-type:text/html;charset=utf-8"); //设置字符编码 echo "当前文件的路径:".__FILE__; echo "<br/>当前的行数:".__LINE__; echo "<br/>当前PHP的版本信息:".PHP_VERSION; echo "<br/>当前的操作系统:".PHP_OS; ?>
Example details :
There is no difference between predefined constants and In the above example, use the PHP predefined constant __FILE__ to obtain The path to the current PHP file; the PHP predefined constant __LINE__ gets the line number of the program; the PHP_VERSION predefined constant gets the current PHP version, and the PHP_OS predefined constant gets the current operating system.variables" in PHP
Recommended related video tutorials: "php.cn Dugu Jiujian ( 4)-php video tutorial:Predefined variables and constants
The above is the detailed content of Detailed explanation of PHP predefined constant examples. For more information, please follow other related articles on the PHP Chinese website!