探讨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 相关文章推荐
php 中include()与require()的对比
Oct 09 PHP
PHP下编码转换函数mb_convert_encoding与iconv的使用说明
Dec 16 PHP
PHP开发负载均衡指南
Jul 17 PHP
PHP APC缓存配置、使用详解
Mar 06 PHP
CI框架给视图添加动态数据
Dec 01 PHP
php判断当前用户已在别处登录的方法
Jan 06 PHP
PHP获取当前日期和时间及格式化方法参数
May 11 PHP
php二维码生成
Oct 19 PHP
Zend Framework实现留言本分页功能(附demo源码下载)
Mar 22 PHP
php自定义函数实现二维数组按指定key排序的方法
Sep 29 PHP
php生成0~1随机小数的方法(必看)
Apr 05 PHP
php实现微信公众号企业转账功能
Oct 01 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
php中根据某年第几天计算出日期年月日的代码
2011/02/24 PHP
php 冒泡排序 交换排序法
2011/05/10 PHP
php中文乱码怎么办如何让浏览器自动识别utf-8
2014/01/15 PHP
让ie运行js时提示允许阻止内容运行的解决方法
2010/10/24 Javascript
Javascript中的delete介绍
2012/09/02 Javascript
javascript中不提供sleep功能如何实现这个功能
2014/05/27 Javascript
jquery实现多条件筛选特效代码分享
2015/08/28 Javascript
javascript获取指定区间范围随机数的方法
2017/09/08 Javascript
详解Vue SPA项目优化小记
2018/07/03 Javascript
vue发送websocket请求和http post请求的实例代码
2019/07/11 Javascript
vux-scroller实现移动端上拉加载功能过程解析
2019/10/08 Javascript
Js跳出两级循环方法代码实例
2020/09/22 Javascript
[59:35]DOTA2上海特级锦标赛主赛事日 - 3 败者组第三轮#1COL VS Alliance第二局
2016/03/04 DOTA
python实现巡检系统(solaris)示例
2014/04/02 Python
Python判断直线和矩形是否相交的方法
2015/07/14 Python
python win32 简单操作方法
2017/05/25 Python
python将unicode转为str的方法
2017/06/21 Python
rabbitmq(中间消息代理)在python中的使用详解
2017/12/14 Python
Python分割指定页数的pdf文件方法
2018/10/26 Python
python对视频画框标记后保存的方法
2018/12/07 Python
TensorFlow实现从txt文件读取数据
2020/02/05 Python
keras训练曲线,混淆矩阵,CNN层输出可视化实例
2020/06/15 Python
CSS3 制作旋转的大风车(充满童年回忆)
2013/01/30 HTML / CSS
HTML5拖放功能_动力节点Java学院整理
2017/07/13 HTML / CSS
嘻哈珠宝品牌:KRKC&CO
2020/10/19 全球购物
教师简历自我评价
2014/02/03 职场文书
经典而简洁的婚礼主持词
2014/03/13 职场文书
法律七进实施方案
2014/03/15 职场文书
区域销售主管岗位职责
2014/06/15 职场文书
房屋出租协议书范本(标准版)
2014/09/24 职场文书
2014年租房协议书范本
2014/10/30 职场文书
仓库统计员岗位职责
2015/04/14 职场文书
2015年话务员工作总结
2015/04/29 职场文书
2015年庆祝国庆节66周年演讲稿
2015/07/30 职场文书
《敬重卑微》读后感3篇
2019/11/26 职场文书
Docker部署Mysql8的实现步骤
2022/07/07 Servers