探讨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 相关文章推荐
用libtemplate实现静态网页生成
Oct 09 PHP
PHP取二进制文件头快速判断文件类型的实现代码
Aug 05 PHP
php mysql_real_escape_string函数用法与实例教程
Sep 30 PHP
成为好程序员必须避免的5个坏习惯
Jul 04 PHP
php获取YouTube视频信息的方法
Feb 11 PHP
thinkPHP模板中函数的使用方法示例
Nov 30 PHP
基于ThinkPHP实现的日历功能实例详解
Apr 15 PHP
PHP基于XMLWriter操作xml的方法分析
Jul 17 PHP
PHP实现的获取文件mimes类型工具类示例
Apr 08 PHP
PHP基于面向对象封装的分页类示例
Mar 15 PHP
Laravel5.1框架注册中间件的三种场景详解
Jul 09 PHP
php使用goto实现自动重启swoole、reactphp、workerman服务的代码
Apr 13 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模拟SQL Server的两个日期处理函数
2006/10/09 PHP
php中的注释、变量、数组、常量、函数应用介绍
2012/11/16 PHP
PHP实现用户异地登录提醒功能的方法【基于thinkPHP框架】
2018/03/15 PHP
将字符串中由空格隔开的每个单词首字母大写
2014/04/06 Javascript
JS判断客户端是手机还是PC的2个代码
2014/04/12 Javascript
随鼠标移动的时钟非常漂亮遗憾的是只支持IE
2014/08/12 Javascript
AngularJS基础学习笔记之表达式
2015/05/10 Javascript
jQuery替换textarea中换行的方法
2015/06/10 Javascript
jQuery使用Layer弹出层插件闪退问题
2016/12/22 Javascript
js实现横向拖拽导航条功能
2017/02/17 Javascript
addeventlistener监听scroll跟touch(实例讲解)
2017/08/04 Javascript
React-Router如何进行页面权限管理的方法
2017/12/06 Javascript
浅析vue中常见循环遍历指令的使用 v-for
2018/04/18 Javascript
如何从0开始用node写一个自己的命令行程序
2018/12/29 Javascript
element form 校验数组每一项实例代码
2019/10/10 Javascript
[06:11]2014DOTA2国际邀请赛 专访团结一心的VG战队
2014/07/21 DOTA
[36:05]DOTA2亚洲邀请赛 3.31 小组赛 A组 Liquid vs Optic
2018/04/01 DOTA
基于Python实现的扫雷游戏实例代码
2014/08/01 Python
浅析Python中signal包的使用
2015/11/13 Python
Python编写登陆接口的方法
2017/07/10 Python
详解python解压压缩包的五种方法
2019/07/05 Python
Python字符串中添加、插入特定字符的方法
2019/09/10 Python
Django REST framework 单元测试实例解析
2019/11/07 Python
Android Q之气泡弹窗的实现示例
2020/06/23 Python
Python实现爬取网页中动态加载的数据
2020/08/17 Python
Mio Skincare法国官网:身体紧致及孕期身体护理
2018/04/04 全球购物
英国网上自行车商店:Tredz Bikes
2019/10/29 全球购物
法国春天百货官网:Printemps.com
2020/06/29 全球购物
酒店个人培训自我鉴定
2013/12/11 职场文书
审计局2014法制宣传日活动总结
2014/11/01 职场文书
2014年公务员转正工作总结
2014/11/07 职场文书
2014年个人技术工作总结
2014/12/08 职场文书
高温慰问简报
2015/07/21 职场文书
篮球拉拉队口号
2015/12/25 职场文书
2016年优秀党务工作者先进事迹材料
2016/02/29 职场文书
mongoDB数据库索引快速入门指南
2022/03/23 MongoDB