可以使用defined()----检测常量是否设置

【问】在php中定义常量时,const与define的区别?

【答】使用const使得代码简单易读,const本身就是一个语言结构,而define是一个函数。另外const在编译时要比define快很多。

(1).const用于类成员变量的定义,一经定义,不可修改。define不可用于类成员变量的定义,可用于全局常量。

(2).const可在类中使用,define不能。

(3).const不能在条件语句中定义常量。

例如:

if (...){

const FOO = 'BAR';  // 无效的invalid

}

if (...) {

define('FOO', 'BAR'); // 有效的valid

}

(4).const采用一个普通的常量名称,define可以采用表达式作为名称。 

const  FOO = 'BAR';

for ($i = 0; $i < 32; ++$i) {

define('BIT_' . $i, 1 << $i);

}

(5).const只能接受静态的标量,而define可以采用任何表达式。

例如:

const BIT_5 = 1 << 5;  // 无效的invalid

define('BIT_5', 1 << 5); // 有效的valid

(6).const定义的常量时大小写敏感的,而define可通过第三个参数(为true表示大小写不敏感)来指定大小写是否敏感。

例如:

define('FOO', 'BAR', true);

echo FOO; // BAR

echo foo; // BAR

const define区别的更多相关文章

  1. php const define 区别有那些呢?

    (1) 编译器处理方式不同 define宏是在预处理阶段展开. const常量是编译运行阶段使用. (2) 类型和安全检查不同 define宏没有类型,不做任何类型检查,仅仅是展开. const常量有 ...

  2. const & define & inline

    0x01  const & define区别 宏定义#define发生在预编译期,而const,enum定义的常量发生在编译期,两者的重要差别在于编译期里的变量是进符号表的,而预编译期的宏是简 ...

  3. const 和 #define区别_fenglovel_新浪博客

    const 和 #define区别 (2012-12-11 14:14:07) 转载▼ 标签: 杂谈   (1) 编译器处理方式不同 define宏是在预处理阶段展开. const常量是编译运行阶段使 ...

  4. C++(十九) — const 和 #define 区别

    1.const  (1)C++对 const 常量的处理过程:当编译器碰到 常量声明 时,在符号表中放入常量,编译时发现使用常量,则直接以符号表中的值替换. (2)如果,编译中发现,对 const 使 ...

  5. 20140808 const和define区别 内联函数(inline) 栈和堆的地址分配 栈帧

    1.const和define区别 const有数据类型(不能改变的变量),define只是简单的字符串替换,没有数据类型. C++程序用const完全取代 define. const还可以类成员函数为 ...

  6. #define 和常量 const 的区别

    const 后的常量,程序对其中只能读不能修改. #include <iostream> using namespace std; int main() { const double pi ...

  7. iOS开发之--宏定义与const的区别及使用方法

    宏定义的常见用法: 定义一段代码,或指定字符串抽成宏. const(常量): 当有字符串常量的时候,苹果推荐我们使用const,苹果经常把常用的字符串定义成const 宏定义与const的区别: 编译 ...

  8. const define static extern

    const const意味着"只读",欲阻止一个变量被改变,可以使用const关键字 const仅仅用来修饰右边的变量(基本数据变量p,指针变量*p) define #define ...

  9. readonly和const的区别

    readonly与const的区别1.const常量在声明的同时必须赋值,readonly在声明时可以不赋值2.readonly只能在声明时或在构造方法中赋值(readonly的成员变量可以根据调用不 ...

随机推荐

  1. redis实现session共享,哨兵

    一.Redis介绍 1.redis是key-value的存储系统,属于非关系型数据库 2.特点:支持数据持久化,可以让数据在内存中保存到磁盘里(memcached:数据存在内存里,如果服务重启,数据会 ...

  2. java: jdk1.8以后就不支持桥接的方式

    java: jdk1.8以后就不支持桥接的方式 如果想继续使用桥接的方式,请使用jdk1.7及以下版本.

  3. MVC5中EF6 Code First启动慢及间隙变慢的一些优化处理

    问题描述: 第一次访问的时候很慢,后面再次打开页面很快,过了一段时间不访问页面然后再次打开页面又像第一次那样很慢. 采用的技术和环境: 使用技术:EF6+MVC5 服务器环境:Windows 2012 ...

  4. BeanUtils介绍及使用

    JavaBeans事实上有三层含义.首先,JavaBeans是一种规范,一种在Java(包括JSP)中可重复使用的Java组件的技术规范,也可以说成我们常说的接口.其次,JavaBeans是一个Jav ...

  5. 1103 Integer Factorization (30)(30 分)

    The K-P factorization of a positive integer N is to write N as the sum of the P-th power of K positi ...

  6. ACM学习历程—HDU5407 CRB and Candies(数论)

    Problem Description CRB has N different candies. He is going to eat K candies.He wonders how many co ...

  7. 【LeetCode】080. Remove Duplicates from Sorted Array II

    题目: Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For ex ...

  8. POJ 1503 Integer Inquiry(大数相加)

    一.Description One of the first users of BIT's new supercomputer was Chip Diller. He extended his exp ...

  9. Poj1159 Palindrome(动态规划DP求最大公共子序列LCS)

    一.Description A palindrome is a symmetrical string, that is, a string read identically from left to ...

  10. MongoDB分析工具之一:explain()语句分析工具

    explain(),语句分析工具 MongoDB 3.0之后,explain的返回与使用方法与之前版本有了很大的变化,介于3.0之后的优秀特色和我们目前所使用给的是3.0.7版本,本文仅针对Mongo ...