探讨php define()函数及defined()函数使用详解

所属分类: 网络编程 / PHP编程 阅读数: 2035
收藏 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实现的美国50个州选择列表实例

这篇文章主要介绍了php实现的美国50个州选择列表实例,可实现让当前州为选中状态的功能,非常具有实用价值,需要的朋友可以参考下
收藏 0 赞 0 分享

PHP使用递归生成文章树

写递归函数,可考虑缓存,定义一些静态变量来存上一次运行的结果,多程序运行效率很有帮助.大概步骤如下:首先到数据库取数据,放到一个数组,然后把数据转化为一个树型状的数组,最后把这个树型状的数组转为html代码。下面我们来看个实例
收藏 0 赞 0 分享

wordpress安装过程中遇到中文乱码的处理方法

这篇文章主要介绍了wordpress安装过程中遇到中文乱码的处理方法,是个人项目中遇到的一个奇葩事件,经过一番研究,终于解决,这里记录下来分享给大家,有需要的小伙伴可以参考下。
收藏 0 赞 0 分享

php的crc32函数使用时需要注意的问题(不然就是坑)

这篇文章主要介绍了php的crc32函数使用时需要注意的问题(不然就是坑) ,需要的朋友可以参考下
收藏 0 赞 0 分享

PHP中把对象转换为关联数组代码分享

这篇文章主要介绍了PHP中把对象转换为关联数组代码分享,本文直接给出实现代码,需要的朋友可以参考下
收藏 0 赞 0 分享

php检测url是否存在的方法

这篇文章主要介绍了php检测url是否存在的方法,涉及php中get_headers及正则匹配的技巧,非常具有实用价值,需要的朋友可以参考下
收藏 0 赞 0 分享

php获取twitter最新消息的方法

这篇文章主要介绍了php获取twitter最新消息的方法,涉及php操作curl及正则替换的技巧,非常具有实用价值,需要的朋友可以参考下
收藏 0 赞 0 分享

php遍历CSV类实例

这篇文章主要介绍了php遍历CSV类,实例分析了php针对csv文件的打开、读取及遍历的技巧,非常具有实用价值,需要的朋友可以参考下
收藏 0 赞 0 分享

PHP使用mysqldump命令导出数据库

最近用php写一个数据备份的功能。做法是使用php的system函数执行mysqldump命令,进行备份,这里分享给大家,有需要的小伙伴可以参考下。
收藏 0 赞 0 分享

PHP用反撇号执行外部命令

shell_exec() 命令行实际上仅是反撇号 ` 操作符的变体,如果您编写过 shell 或 Perl 脚本,您就知道可以在反撇号操作符内部捕捉其他命令的输出。
收藏 0 赞 0 分享
查看更多