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";
- }