What are the types of php constants?

青灯夜游
Release: 2023-02-28 16:10:01
Original
3646 people have browsed it

What are the types of php constants?

What are php constants?

The so-called constant is a quantity that cannot be changed. Once a PHP constant is defined, it cannot be changed or undefined. This is the official explanation and the most authoritative explanation.

php constants are usually used to store data that does not change and is not expected to change. The data can only be data of four scalar data types: integer (integer), floating point (decimal), character String, Boolean type (true/false).

Constants are divided into system built-in constants and custom constants.

The most common system built-in constants are __FILE__, __LINE__, PHP_OS, PHP_VERSION, PHP_OS, TRUE, etc.

php Naming rules for custom constants

The naming rules for PHP constants are similar to the naming rules for PHP variables but not exactly the same.

● PHP constant names must be composed of letters, underscores, and numbers;

● They cannot start with numbers;

● Whether to be case-sensitive is specified when defining;

● It is recommended to know the meaning when naming (you will know what it means when you see it), use English words as constant names, and it is recommended that the letters are all capital letters (such as: PI);

Naming of php constants:

Copy after login

Running result:

3.14 3.142
Copy after login
Copy after login

How to define PHP constants?

php constants are defined using the define() function. The define() function has two required parameters and one optional parameter. The first parameter specifies the name of the constant, also called an identifier; the second parameter specifies the value of the constant, which is a scalar data type that does not want to be changed; the third parameter is an optional parameter, used to specify the name of the constant. Is it case sensitive? If TRUE, constant names are case-insensitive; if FALSE (the default), they are case-sensitive.

Use the define() function to define constants:

Copy after login

Running results:

3.14 3.142
Copy after login
Copy after login

Recommended learning: PHP tutorial

The above is the detailed content of What are the types of php constants?. 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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!