Home > Backend Development > PHP Tutorial > Detailed explanation of the use of define() and defined() functions in php

Detailed explanation of the use of define() and defined() functions in php

黄舟
Release: 2023-03-17 14:42:01
Original
2502 people have browsed it

In the previous article, we introduced the use of define() in php, and the difference between define() and const in php. Today we will continue to introduce the define() function and defined() function in php. The usage is analyzed and introduced in detail, please refer to it if you need it!

The define() function defines a constant.
The define() function defines a constant.
Constants are much like variables, except for the following differences:
Constant [constant] has many similarities with variables [variable], so it is easy to confuse; below, we list the constants [constant] The difference between ] and variable [variable]:

•A constant's value cannot be changed after it is set
A constant value cannot be changed after it is specified;
•Constant names do not need a leading dollar sign ($)
When setting constants, there is no need to add the "$" sign in front;
•Constants can be accessed regardless of scope
Constant values ​​can only be strings and numbers
Constant values ​​can only be strings and numbers String
[string]" and "number[number]";

Syntax

Syntax

The code is as follows:

define(name,value,case_insensitive)
Copy after login

ParameterParameternamed. Specifies the name of the constantvalueRequired. Specifies the value of the constantcase_insensitiveOptional. Specifies whether the constant name should be case-insensitive. If set to TRUE, the constant will be case-insensitive. Default is FALSE (case-sensitive)
Description
Description

RequireRequired parameters. Specifies the name of the constant
Required parameter. Specifies the value of the constant
Optional parameters. Specifies whether the name of the constant is case-insensitive. If set to True, letters are not case-sensitive; if set to False, letters are case-sensitive. The default value is: False

Example 1Case 1

Define a case-sensitive constant:
Copy after login

Specify a constant (case sensitive):

The code is as follows:

<?phpdefine("GREETING","Hello you! How are you today?");echo constant("GREETING");?>
Copy after login

The output of the code above will be:

The above code will output the following results:

Hello you! How are you today?
Copy after login
Copy after login


Example 2

Case 2
Define a case-insensitive constant:
Specify a constant (case-insensitive):

<?phpdefine("GREETING","Hello you! How are you today?",TRUE);echo constant("greeting");?>
Copy after login
The output of the code above will be:
The above code will output the following results:

Hello you! How are you today?
Copy after login
Copy after login

The defined() function checks whether a constant exists.
defined() The function is to check whether a constant exists.

Returns TRUE if the constant exists, or FALSE otherwise.
If the constant exists, it returns True; if it does not exist, it returns False.



Syntax
Syntax

defined(name)
Copy after login

ParameterParameternameRequired. Specifies the name of the constant to check object

Example
案例

<?phpdefine("GREETING","Hello you! How are you today?");echo defined("GREETING");?>
Copy after login

The output of the code above will be:
上述代码将输出下面的结果:

1
Copy after login

总结:

本文通过示例讲述了php中define()与defined()函数的使用详解,希望对你的工作上有所帮助!

相关推荐:

php常量define与const的区别详解


php中define的使用详解


php define常量定义与变量区别_PHP教程

The above is the detailed content of Detailed explanation of the use of define() and defined() functions in php. 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
Description
Description

Required parameters. Specify the name of the constant