• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

php中define()与defined()函数的使用详解

php 搞代码 3年前 (2022-01-22) 25次浏览 已收录 0个评论

在之前的文章中我们为大家介绍了php中define()的使用,以及php中define()与const的区别,今天为大家继续介绍php中的define()函数以及defined()函数的用法进行了详细的分析介绍,需要的朋友参考下!
/本文来源gao@!dai!ma.com搞$$代^@码5网@搞代gaodaima码

The define() function defines a constant.
define()函数的作用是:定义一个常量。
Constants are much like variables, except for the following differences:
常量[constant]与变量[variable]有很多相似的地方,因此,很容易混淆;下面,我们列举一下常量[constant]与变量[variable]之间的不同点:

•A constant's value cannot be changed after it is set
一个常量值在指定之后就不可以更改;
•Constant names do not need a leading dollar sign ($)
设置常量时,不需要在前面加上“$”符号;
•Constants can be accessed regardless of scope
常量可以被所有范围的域访问;
•Constant values can only be strings and numbers
常量的值只能是“字符串[string]”和“数字[number]”;

Syntax
语法

define(name,value,case_insensitive)
Parameter
参数
Description
描述
name Required. Specifies the name of the constant
必要参数。指定常量的名称
value Required. Specifies the value of the constant
必要参数。指定常量的值
case_insensitive Optional. Specifies whether the constant name should be case-insensitive. If set to TRUE, the constant will be case-insensitive. Default is FALSE (case-sensitive)
可选参数。指定常量的名称是否是不区分大小写的[case-insensitive]。如果设置为True,则不区分字母大小写;如果设置为False,则区分字母大小写。默认值是:False


Example 1
案例1

Define a case-sensitive constant:

指定一个常量(区分大小写):

<?phpdefine("GREETING","Hello you! How are you today?");echo constant("GREETING");?>
The output of the code above will be:<br />上述代码将输出下面的结果:
Hello you! How are you today?

Example 2
案例2
Define a case-insensitive constant:
指定一个常量(不区分大小写):

<?phpdefine("GREETING","Hello you! How are you today?",TRUE);echo constant("greeting");?>
The output of the code above will be:<br />上述代码将输出下面的结果:
Hello you! How are you today?

The defined() function checks whether a constant exists.
defined()函数的作用是:检查一个常量是否存在。

Returns TRUE if the constant exists, or FALSE otherwise.
如果该常量存在,则返回True;如果不存在,则返回False。

Syntax
语法

defined(name)
Parameter
参数
Description
描述
name Required. Specifies the name of the constant to check
必要参数。指定常量对象的名称

Example
案例

<?phpdefine("GREETING","Hello you! How are you today?");echo defined("GREETING");?>
The output of the code above will be:<br />上述代码将输出下面的结果:
1
<span style="font-size: 18px"><strong>总结:</strong></span>
本文通过示例讲述了php中define()与defined()函数的使用详解,希望对你的工作上有所帮助!
<span style="font-size: 18px"><strong>相关推荐:</strong></span>

php常量define与const的区别详解

php中define的使用详解

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

以上就是php中define()与defined()函数的使用详解的详细内容,更多请关注搞代码gaodaima其它相关文章!


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:php中define()与defined()函数的使用详解

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址