What are constants?
Constants can be understood as quantities whose values do not change. Once a constant is defined, it cannot be changed anywhere else in the program script.
How to define constants
Constants are composed of English letters, underscores, and numbers. However, one thing to note is that the first letter of our constants cannot be numbers. started.
In PHP we use the define() function to define constants. The syntax of the define() function is as follows
bool define ($name , $value ,$case_insensitive = false)
Detailed explanation of parameters
This function has three Parameters:
$name: The name of the constant, must have
#$value: The value of the constant, must have
$case_insensitive Used to control constants Whether the name is case-sensitive (the default is sensitive), set true to indicate insensitivity. Optional parameters, there are two values, true and false
Get the constant
There are two ways to get the value of the constant: one is to use the constant name to get the value directly
<?php header("content-type:text/html;charset=utf-8"); //设置字符编码 define('name','PHP中文网'); echo name; ?>
The other is to use the constant() function. The constant() function has the same effect as directly using the constant name to output, but the function can dynamically output different constants, which is much more flexible and convenient to use.
The syntax format of the constant() function is as follows
constant (const_name )
The parameter const_name is the name of the constant to be obtained, or it can be a variable that stores the constant name. If the acquisition is successful, the value of the constant is returned, otherwise an error message is displayed. The constant is not defined.
<?php header("content-type:text/html;charset=utf-8"); //设置字符编码 define("MAXSIZE", 'PHP中文网'); echo constant("MAXSIZE"); ?>
Code running results:
Determine whether the constant is defined
To determine a constant Whether it has been defined, you can use the defined() function. The syntax of the function is as follows
defined(name)
Parameters name is the name of the constant to be obtained. It returns true if successful, otherwise it returns false.
<?php header("content-type:text/html;charset=utf-8"); //设置字符编码 define("GREETING","PHP中文网"); echo defined("GREETING"); ?>
Code running results:
Example
For more information To better understand how to define constants, here is an example of defining constants. Use the above-mentioned define() function, constant() function, and defined() function in the example. Use the define() function to define a constant, use the constant() function to dynamically obtain the value of the constant, and use the defined() function to determine whether the constant is defined. The example code is as follows
<?php header("content-type:text/html;charset=utf-8"); //设置字符编码 define("GREETING","看到PHP中文网一次"); echo GREETING."<br/>"; //输出常量GREETING echo Greeting ."<br/>"; define('COUNT','能看到PHP中文网多次',true); echo COUNT ."<br/>"; //输出常量COUNT echo Count ."<br/>"; //输出常量COUNT,因为设定了大小写不敏感 $name='count'; echo constant($name)."<br/>"; //输出常量COUNT echo(defined('GREETING'))."<br/>"; //如果常量被定义,则返回true,使用echo输出显示1 ?>
The result of running the code:
The above is a simple example where we define, obtain and judge constants through functions. The next section, We explain, "Predefined constants" in PHP.
Recommended related video tutorials: "php.cn Dugu Jiujian (4) - PHP video tutorial": Variables and constants: small warehouse for storing data in the program.
The above is the detailed content of Detailed explanation of the definition and usage examples of PHP constants. For more information, please follow other related articles on the PHP Chinese website!