What does a php constant consist of?

青灯夜游
Release: 2023-02-26 11:08:01
Original
2980 people have browsed it

PHP constant is an identifier of a simple value that cannot be changed in the script; after the constant value is defined, it cannot be changed anywhere else in the script. A constant consists of English letters, underscores, and numbers, but numbers cannot appear as the first letter. (The $ modifier is not required on the constant name).

What does a php constant consist of?

Note: Constants can be used throughout the script.

Set PHP constants

To set constants, use define() function, functionSyntaxis as follows:

bool define ( string $name , mixed $value [, bool $case_insensitive = false ] )
Copy after login

This function has three parameters:

●name: required parameter, constant name, that is, identifier.

● Value: required parameter, the value of a constant.

● case_insensitive: Optional parameter, if set to TRUE, this constant is case-insensitive. The default is case-sensitive.

In the following example we create a case-insensitive constant, the constant value is "Welcome to php.cn":

'; echo greeting; // 输出 "greeting" ?>
Copy after login

In the following example we create a case-insensitive constant, the constant value is "Welcome to php.cn":

Copy after login

Constants are global

After a constant is defined, it defaults to a global variable and can be used throughout the Run the script anywhere it is used.

The following example demonstrates the use of constants within a function. Even if the constant is defined outside the function, the constant can be used normally.

Copy after login

The above is the detailed content of What does a php constant consist of?. 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
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!