Home > Backend Development > PHP Tutorial > Detailed explanation of PHP predefined constant examples

Detailed explanation of PHP predefined constant examples

怪我咯
Release: 2023-03-07 16:36:01
Original
5108 people have browsed it

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:

##PHP_OSBuilt-in constants, perform PHP parsing The operating system name of the device, such as WindowsTRUEThis constant is a true value (true) FALSEThis constant is a false value (false) NULLA null value E_ERRORThis constant points to the nearest error E_WARNINGThis constant points to the nearest warning E_PARSEThis constant points to potential problems with parsing grammar E_NOTICEThis constant is a prompt for unusual occurrences but not necessarily Is the error
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

Note: The "__" in __FILE__ and __LINE__ in the above table are two underscores, not one "_" .Detailed explanation of PHP predefined constant examples

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;
?>
Copy after login

Example details :

There is no difference between predefined constants and

user-defined constants

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.


The results of running the code are as follows:

Detailed explanation of PHP predefined constant examples

#Since the operating system and system version used by each user are different, the results obtained may be different. No, this is obtained based on the actual situation of each user.

After understanding the constants of PHP, the next section will lead you to understand the "

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!

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