探讨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在线代理转向代码
May 05 PHP
浅析memcache启动以及telnet命令详解
Jun 28 PHP
php配合jquery实现增删操作具体实例
Dec 12 PHP
PHP jQuery表单,带验证具体实现方法
Feb 15 PHP
PHP错误和异长常处理总结
Mar 06 PHP
php的mkdir()函数创建文件夹比较安全的权限设置方法
Jul 28 PHP
使用PHP Socket 编程模拟Http post和get请求
Nov 25 PHP
PHP中filter函数校验数据的方法详解
Jul 31 PHP
PHP的关于变量和日期处理的一些面试题目整理
Aug 10 PHP
PHP扩展Memcache分布式部署方案
Dec 06 PHP
PDO::exec讲解
Jan 28 PHP
PHP PDOStatement::rowCount讲解
Feb 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
jq的get传参数在utf-8中乱码问题的解决php版
2008/07/23 PHP
PHP转盘抽奖接口实例
2015/02/09 PHP
PHP+Mysql基于事务处理实现转账功能的方法
2015/07/08 PHP
CI框架源码解读之URI.php中_fetch_uri_string()函数用法分析
2016/05/18 PHP
PHP之十六个魔术方法详细介绍
2016/11/01 PHP
php实现留言板功能
2017/03/05 PHP
PHP的图像处理实例小结【文字水印、图片水印、压缩图像等】
2019/12/20 PHP
自动更新作用
2006/10/08 Javascript
关于COOKIE个数与大小的问题
2011/01/17 Javascript
基于 Docker 开发 NodeJS 应用
2014/07/30 NodeJs
javascript中2个感叹号的用法实例详解
2014/09/04 Javascript
windows下安装nodejs及框架express
2015/08/07 NodeJs
微信小程序 倒计时组件实现代码
2016/10/24 Javascript
使用cropper.js裁剪头像的实例代码
2017/09/29 Javascript
监控微信小程序中的慢HTTP请求过程详解
2019/07/05 Javascript
Threejs实现滴滴官网首页地球动画功能
2020/07/13 Javascript
vue 路由meta 设置导航隐藏与显示功能的示例代码
2020/09/04 Javascript
前端如何实现动画过渡效果
2021/02/05 Javascript
python中MySQLdb模块用法实例
2014/11/10 Python
Python编程对列表中字典元素进行排序的方法详解
2017/05/26 Python
Python利用pandas计算多个CSV文件数据值的实例
2018/04/19 Python
Python调用C++,通过Pybind11制作Python接口
2018/10/16 Python
解决Django中checkbox复选框的传值问题
2020/03/31 Python
Python中使用aiohttp模拟服务器出现错误问题及解决方法
2020/10/31 Python
python爬虫中抓取指数的实例讲解
2020/12/01 Python
HTML5时代CSS设置漂亮字体取代图片
2014/09/04 HTML / CSS
是什么让J2EE适合用来开发多层的分布式的应用
2015/01/16 面试题
工地宣传标语
2014/06/18 职场文书
2014年社区矫正工作总结
2014/11/18 职场文书
法院个人总结
2015/03/03 职场文书
退货证明模板
2015/06/23 职场文书
辅导员学期工作总结
2015/08/14 职场文书
学习商务礼仪心得体会
2016/01/22 职场文书
fastdfs+nginx集群搭建的实现
2021/03/31 Servers
Python图像处理库PIL详细使用说明
2022/04/06 Python
Mysql中@和@@符号的详细使用指南
2022/06/05 MySQL