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).
Note: Constants can be used throughout the script.
Set PHP constants
To set constants, use define() function, functionSyntax is as follows:
bool define ( string $name , mixed $value [, bool $case_insensitive = false ] )
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":
<?php // 区分大小写的常量名 define("GREETING", "欢迎访问 php.cn"); echo GREETING; // 输出 "欢迎访问 php.cn" echo '<br>'; echo greeting; // 输出 "greeting" ?>
In the following example we create a case-insensitive constant, the constant value is "Welcome to php.cn":
<?php // 不区分大小写的常量名 define("GREETING", "欢迎访问 php.cn", true); echo greeting; // 输出 "欢迎访问 php.cn" ?>
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.
<?php define("GREETING", "欢迎访问 php.cn"); function myTest() { echo GREETING; } myTest(); // 输出 "欢迎访问 php.cn" ?>
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

SublimeText3 Linux new version
SublimeText3 Linux latest version

WebStorm Mac version
Useful JavaScript development tools
