首页 >后端开发 >php教程 > 正文

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

原创2017-11-15 11:00:410943
在之前的文章中我们为大家介绍了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
描述
nameRequired. Specifies the name of the constant
必要参数。指定常量的名称
valueRequired. Specifies the value of the constant
必要参数。指定常量的值
case_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)
可选参数。指定常量的名称是否是不区分大小写的[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
描述
nameRequired. 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()函数的使用详解的详细内容,更多请关注php中文网其它相关文章!

php中文网最新课程二维码

声明:本文原创发布php中文网,转载请注明出处,感谢您的尊重!如有疑问,请联系admin@php.cn处理

  • 相关标签:defined define php
  • 相关文章

    相关视频


    网友评论

    文明上网理性发言,请遵守 新闻评论服务协议

    我要评论
  • 专题推荐

    作者信息

    黄舟

    人生最曼妙的风景,竟是内心的淡定与从容!

    最近文章
    php实现字符串匹配算法之sunday算法的示例3303
    css中关于hideFocus的使用详解3350
    JavaScript中filter函数的详细介绍3122
    推荐视频教程
  • javascript初级视频教程javascript初级视频教程
  • jquery 基础视频教程jquery 基础视频教程
  • 视频教程分类