您好,欢迎访问一九零五行业门户网

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

在之前的文章中我们为大家介绍了php中define()的使用,以及php中define()与const的区别,今天为大家继续介绍php中的define()函数以及defined()函数的用法进行了详细的分析介绍,需要的朋友参考下!
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中define()与defined()函数的使用详解,希望对你的工作上有所帮助!
相关推荐:
php常量define与const的区别详解
php中define的使用详解
php define常量定义与变量区别_php教程
以上就是php中define()与defined()函数的使用详解的详细内容。
其它类似信息

推荐信息