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


Posted in PHP onJune 09, 2013

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 相关文章推荐
经典的PHPer为什么被认为是草根?
Apr 02 PHP
简单PHP上传图片、删除图片实现代码
May 12 PHP
在wamp集成环境下升级php版本(实现方法)
Jul 01 PHP
CI框架中zip类应用示例
Jun 17 PHP
php实现webservice实例
Nov 06 PHP
php内嵌函数用法实例
Mar 20 PHP
分享自定义的几个PHP功能函数
Apr 15 PHP
php微信开发之带参数二维码的使用
Aug 03 PHP
php使用FFmpeg接口获取视频的播放时长、码率、缩略图以及创建时间
Nov 07 PHP
php判断str字符串是否是xml格式数据的方法示例
Jul 26 PHP
Yii2框架可逆加密简单实现方法
Aug 25 PHP
[原创]PHP实现字节数Byte转换为KB、MB、GB、TB的方法
Aug 31 PHP
如何用php获取程序执行的时间
Jun 09 #PHP
php生成zip压缩文件的方法详解
Jun 09 #PHP
基于php iconv函数的使用详解
Jun 09 #PHP
PHP页面间参数传递的四种方法详解
Jun 09 #PHP
利用PHP+JS实现搜索自动提示(实例)
Jun 09 #PHP
深入php处理整数函数的详解
Jun 09 #PHP
解决ajax+php中文乱码的方法详解
Jun 09 #PHP
You might like
WordPress的主题编写中获取头部模板和底部模板
2015/12/28 PHP
CI框架中redis缓存相关操作文件示例代码
2016/05/17 PHP
PHP实现ASCII码与字符串相互转换的方法
2017/04/29 PHP
MooTools 页面滚动浮动层智能定位实现代码
2011/08/23 Javascript
jquery 延迟执行实例介绍
2013/08/20 Javascript
node.js解决获取图片真实文件类型的问题
2014/12/20 Javascript
jquery.gridrotator实现响应式图片展示画廊效果
2015/06/23 Javascript
JS动态添加iframe的代码
2015/09/14 Javascript
jquery实现的回旋滚动效果完整实例【附demo源码下载】
2016/09/20 Javascript
d3.js实现简单的网络拓扑图实例代码
2016/11/06 Javascript
使用Bootstrap + Vue.js实现添加删除数据示例
2017/02/27 Javascript
vue.js的vue-cli脚手架中使用百度地图API的实例
2019/01/21 Javascript
关于layui flow loading占位图的实现方法
2019/09/21 Javascript
Vue-router编程式导航的两种实现代码
2021/03/04 Vue.js
Python2.x和3.x下maketrans与translate函数使用上的不同
2015/04/13 Python
python fabric使用笔记
2015/05/09 Python
Python 通配符删除文件的实例
2018/04/24 Python
matplotlib 纵坐标轴显示数据值的实例
2018/05/25 Python
Python退火算法在高次方程的应用
2018/07/26 Python
python基础梳理(一)(推荐)
2019/04/06 Python
python 如何把docker-compose.yaml导入到数据库相关条目里
2021/01/15 Python
CSS3动画特效在活动页中的应用
2020/01/21 HTML / CSS
Maisons du Monde德国:法国家具和装饰的市场领导者
2019/07/26 全球购物
Farfetch澳大利亚官网:Farfetch Australia
2020/04/26 全球购物
犹他州最古老的体育用品公司:Al’s
2020/12/18 全球购物
PHP开发工程师面试问题集锦
2012/11/01 面试题
大学班级学风建设方案
2014/05/01 职场文书
温馨提示标语
2014/06/26 职场文书
装修施工安全责任书
2014/07/24 职场文书
党支部活动策划方案
2014/08/18 职场文书
大学生党校培训心得体会
2014/09/11 职场文书
工艺技术员岗位职责
2015/02/04 职场文书
专家推荐信怎么写
2015/03/25 职场文书
土建技术员岗位职责
2015/04/11 职场文书
班主任工作经验交流会总结
2015/11/02 职场文书
在pyCharm中下载第三方库的方法
2021/04/18 Python