Blogger Information
Blog 9
fans 0
comment 0
visits 14509
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
在php中定义常量时,const和define的区别?
小张的博客
Original
765 people have browsed it

1、const用于类成员变量的定义,一经定义,不可修改。Define不可以用于类成员变量的定义,可用于全局常量。

2、Const可在类中使用,define不能

3、Const不能再条件语句中定义常量

  1. <?php
  2. if (...){
  3. const FOO = 'BAR'; // 无效的invalid
  4. }
  5. if (...) {
  6. define('FOO', 'BAR'); // 有效的valid
  7. }
  8. ?>

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

  1. <?php
  2. define('FOO', 'BAR', true);
  3. echo FOO; // BAR
  4. echo foo; // BAR
  5. ?>
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post