深入php define()函数以及defined()函数的用法详解


Posted in PHP onJune 05, 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 程序员也要学会使用“异常”
Jun 16 PHP
php 论坛采集程序 模拟登陆,抓取页面 实现代码
Jul 09 PHP
php smarty模版引擎中变量操作符及使用方法
Dec 11 PHP
PHP游戏编程25个脚本代码
Feb 08 PHP
PHP 数据结构 算法 三元组 Triplet
Jul 02 PHP
PHP实现懒加载的方法
Mar 07 PHP
php中使用base HTTP验证的方法
Apr 20 PHP
PHP去掉json字符串中的反斜杠\及去掉双引号前的反斜杠
Sep 30 PHP
解决php的“It is not safe to rely on the system’s timezone settings”问题
Oct 08 PHP
深入浅析yii2-gii自定义模板的方法
Apr 26 PHP
ThinkPHP中类的构造函数_construct()与_initialize()的区别详解
Mar 13 PHP
PHP实现的ID混淆算法类与用法示例
Aug 10 PHP
基于PHP array数组的教程详解
Jun 05 #PHP
深入php list()函数的详解
Jun 05 #PHP
深入php var_dump()函数的详解
Jun 05 #PHP
php数组(array)输出的三种形式详解
Jun 05 #PHP
php set_time_limit()函数的使用详解
Jun 05 #PHP
基于PHP文件操作的详解
Jun 05 #PHP
深入探讨:PHP使用数据库永久连接方式操作MySQL的是与非
Jun 05 #PHP
You might like
重置版游戏视频
2020/04/09 魔兽争霸
PHP中冒号、endif、endwhile、endfor使用介绍
2010/04/28 PHP
CodeIgniter配置之database.php用法实例分析
2016/01/20 PHP
php实现博客,论坛图片防盗链的方法
2016/10/15 PHP
在laravel中使用with实现动态添加where条件
2019/10/10 PHP
php生成短网址/短链接原理和用法实例分析
2020/05/29 PHP
静态页面的值传递(三部曲)
2006/09/25 Javascript
javascript 硬盘序列号+其它硬件信息
2008/12/23 Javascript
BootStrap 动态添加验证项和取消验证项的实现方法
2016/09/28 Javascript
canvas滤镜效果实现代码
2017/02/06 Javascript
vue组件父子间通信之综合练习(聊天室)
2017/11/07 Javascript
微信小程序使用toast消息对话框提示用户忘记输入用户名或密码功能【附源码下载】
2017/12/09 Javascript
jQuery实现表单动态添加与删除数据操作示例
2018/07/03 jQuery
vue 中 命名视图的用法实例详解
2019/08/14 Javascript
vue 解决computed修改data数据的问题
2019/11/06 Javascript
vue通过过滤器实现数据格式化
2020/07/20 Javascript
[59:26]DOTA2上海特级锦标赛D组资格赛#1 EG VS VP第二局
2016/02/28 DOTA
学习python之编写简单乘法口诀表实现代码
2016/02/27 Python
Python Numpy 数组的初始化和基本操作
2018/03/13 Python
pyqt5让图片自适应QLabel大小上以及移除已显示的图片方法
2019/06/21 Python
详解Python中namedtuple的使用
2020/04/27 Python
css3实例教程 一款纯css3实现的环形导航菜单
2014/10/20 HTML / CSS
CSS3制作彩色进度条样式的代码示例分享
2016/06/23 HTML / CSS
爱尔兰最大的体育零售商:Life Style Sports
2019/06/12 全球购物
Shopping happy life西班牙:以最优惠的价格提供最好的时尚配饰
2020/03/13 全球购物
Nike瑞士官网:Nike CH
2021/01/18 全球购物
JSF的标签库有哪些
2012/04/27 面试题
护理不良事件检讨书
2014/02/06 职场文书
书香家庭事迹材料
2014/05/09 职场文书
纪念九一八事变演讲稿:青少年应树立远大理想
2014/09/14 职场文书
大学生考试作弊检讨书
2014/09/21 职场文书
学生上课看漫画的检讨书
2014/09/26 职场文书
房屋租赁委托书范本
2014/10/04 职场文书
在校学生证明格式
2015/06/24 职场文书
投诉信回复范文
2015/07/03 职场文书
超市主管竞聘书
2015/09/15 职场文书