Detailed explanation of the definition and usage examples of PHP constants

怪我咯
Release: 2023-03-07 16:22:02
Original
5558 people have browsed it

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)
Copy after login

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(&#39;name&#39;,&#39;PHP中文网&#39;);
echo name;
?>
Copy after login

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 )
Copy after login

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", &#39;PHP中文网&#39;);

echo constant("MAXSIZE");
?>
Copy after login

Code running results:

Detailed explanation of the definition and usage examples of PHP constants

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)
Copy after login

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

Code running results:

Detailed explanation of the definition and usage examples of PHP constants

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(&#39;COUNT&#39;,&#39;能看到PHP中文网多次&#39;,true);
echo COUNT ."<br/>";                                        //输出常量COUNT
echo Count ."<br/>";                                        //输出常量COUNT,因为设定了大小写不敏感

$name=&#39;count&#39;;
echo constant($name)."<br/>";                               //输出常量COUNT
echo(defined(&#39;GREETING&#39;))."<br/>";                          //如果常量被定义,则返回true,使用echo输出显示1

?>
Copy after login

The result of running the code:

Detailed explanation of the definition and usage examples of PHP constants

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!

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!