1.define用来定义一个常量,常量也是全局范围的。不用管作用域就可以在脚本的任何地方访问
常量。一个常量一旦被定义,就不能再改变或者取消定义
如:
define("path","root/www/web") define为常root/www/web 为常量的值
2.defined用来检测常量有没有被定义,若常量存在,则返回true,否则返回 false如:
[html] view plain copy
- if(defined("path")){
- echo"true";
- }else{
- echo "false";
- }
Yii中
defined('YII_DEBUG') or define('YII_DEBUG', true);
上面的代码等同于:
-
if (!defined('YII_DEBUG')) {
-
define('YII_DEBUG', true);
-
}
显然第一段代码更加简洁易懂。
关于上段代码中的 “or”的用法:
1.逻辑运算的运算顺序是从左向右
2.or运算符的左结合性
3.or运算中,如果运算时有值为真了,则整个表达式为真,后面不需要运算
终合上面三个因素,当or左侧函数运行正常(返回非假值),则不会执行后面的函数
这里利用了or的这个特性,将die(终止并输出函数)用于此处,巧妙的实现了函数异常而报错的功能
3.isset()判断一个变量是否定义
[html] view plain copy
- <span style="font-size:14px;"> if(isset($var)) {
- print "This var is set set so Iwill print.";
- }</span>
4.function_exists判断一个函数是否定义
[html] view plain copy
- <span style="font-size:14px;"> if(function_exists('date_default_timezone_set'))//检查函数是否定义
- {
- date_default_timezone_set("Asia/Chongqing");
- }</span>
5.file_exists判断一个文件是否存在
[php] view plain copy
- <?php
- $file = '/www/index.php';
- if (file_exists($file)) {
- echo "$file";
- } else {
- echo"false";
- }