登录  /  注册

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

黄舟
发布: 2017-11-15 11:00:41
原创
2177人浏览过

在之前的文章中我们为大家介绍了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中文网其它相关文章!

智能AI问答
PHP中文网智能助手能迅速回答你的编程问题,提供实时的代码和解决方案,帮助你解决各种难题。不仅如此,它还能提供编程资源和学习指导,帮助你快速提升编程技能。无论你是初学者还是专业人士,AI智能助手都能成为你的可靠助手,助力你在编程领域取得更大的成就。
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
关于CSS思维导图的课件在哪? 课件
凡人来自于2024-04-16 10:10:18
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2024 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号