深入php define()函数以及defined()函数的用法详解

所属分类: 网络编程 / PHP编程 阅读数: 878
收藏 0 赞 0 分享

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:
上述代码将输出下面的结果:
复制代码 代码如下:

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:
上述代码将输出下面的结果:
复制代码 代码如下:

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:
上述代码将输出下面的结果:
复制代码 代码如下:

1

更多精彩内容其他人还在看

php导出CSV抽象类实例

这篇文章主要介绍了php导出CSV抽象类及其用法示例,可实现循环导出功能,从而避免内存不足的问题,需要的朋友可以参考下
收藏 0 赞 0 分享

php实现的zip文件内容比较类

这篇文章主要介绍了php实现的zip文件内容比较类及其用法,可实现比较两个zip文件的内容,返回新增、删除、及相同的文件列表,是非常实用的技巧,需要的朋友可以参考下
收藏 0 赞 0 分享

php实现获取及设置用户访问页面语言类

这篇文章主要介绍了php实现获取及设置用户访问页面语言类,可实现获取/设置用户访问的页面语言,如果用户没有设置访问语言,则读取Accept-Language,需要的朋友可以参考下
收藏 0 赞 0 分享

PHP+FFMPEG实现将视频自动转码成H264标准Mp4文件

最近做一个在线教学网的项目,需要实现上传任意格式视频自动为h264标准视频,使用html5播放。最终使用PHP+FFMPEG实现,在此将详细解决方案分享给大家!
收藏 0 赞 0 分享

PHP会话控制:Session与Cookie详解

这篇文章主要介绍了PHP会话控制:Session与Cookie详解,本文详细讲解了PHP中Session与Cookie的相关知识,涵盖面较广,需要的朋友可以参考下
收藏 0 赞 0 分享

PHP实现手机号码中间四位用星号(*)隐藏的自定义函数分享

这篇文章主要介绍了PHP实现手机号码中间四位用星号(*)隐藏的自定义函数分享,这是一个比较常用的功能,需要的朋友可以参考下
收藏 0 赞 0 分享

PHP获取mysql数据表的字段名称和详细信息的方法

这篇文章主要介绍了PHP获取mysql数据表的字段名称和详细信息的方法,本文同时还给出了获取数据表结构、列出数据库数据表等方法,需要的朋友可以参考下
收藏 0 赞 0 分享

PHP中的output_buffering详细介绍

这篇文章主要介绍了PHP中的output_buffering详细介绍,本文讲解了output buffering的一些高级用法,需要的朋友可以参考下
收藏 0 赞 0 分享

PHP错误Warning: Cannot modify header information - headers already sent by解决方法

这篇文章主要介绍了PHP错误Warning: Cannot modify header information - headers already sent by解决方法,需要的朋友可以参考下
收藏 0 赞 0 分享

PHP常用编译参数中文说明

这篇文章主要介绍了PHP常用编译参数中文说明,本文用详细的中文注解了PHP编译参数的作用,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多