探討php define()函數(shù)及defined()函數(shù)使用詳解
The define() function defines a constant.
define()函數(shù)的作用是:定義一個(gè)常量。
Constants are much like variables, except for the following differences:
常量[constant]與變量[variable]有很多相似的地方,因此,很容易混淆;下面,我們列舉一下常量[constant]與變量[variable]之間的不同點(diǎn):
A constant's value cannot be changed after it is set
一個(gè)常量值在指定之后就不可以更改;
Constant names do not need a leading dollar sign ($)
設(shè)置常量時(shí),不需要在前面加上“$”符號(hào);
Constants can be accessed regardless of scope
常量可以被所有范圍的域訪問;
Constant values can only be strings and numbers
常量的值只能是“字符串[string]”和“數(shù)字[number]”;
Syntax
語法
define(name,value,case_insensitive)
Parameter 參數(shù) |
Description 描述 |
---|---|
name | Required. Specifies the name of the constant 必要參數(shù)。指定常量的名稱 |
value | Required. Specifies the value of the constant 必要參數(shù)。指定常量的值 |
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) 可選參數(shù)。指定常量的名稱是否是不區(qū)分大小寫的[case-insensitive]。如果設(shè)置為True,則不區(qū)分字母大小寫;如果設(shè)置為False,則區(qū)分字母大小寫。默認(rèn)值是:False |
Example 1
案例1
Define a case-sensitive constant:
指定一個(gè)常量(區(qū)分大小寫):
<?phpdefine("GREETING","Hello you! How are you today?");echo constant("GREETING");?>
The output of the code above will be:
上述代碼將輸出下面的結(jié)果:
Hello you! How are you today?
Example 2
案例2
Define a case-insensitive constant:
指定一個(gè)常量(不區(qū)分大小寫):
<?phpdefine("GREETING","Hello you! How are you today?",TRUE);echo constant("greeting");?>
The output of the code above will be:
上述代碼將輸出下面的結(jié)果:
Hello you! How are you today?
The defined() function checks whether a constant exists.
defined()函數(shù)的作用是:檢查一個(gè)常量是否存在。
Returns TRUE if the constant exists, or FALSE otherwise.
如果該常量存在,則返回True;如果不存在,則返回False。
Syntax
語法
defined(name)
Parameter 參數(shù) |
Description 描述 |
---|---|
name | Required. Specifies the name of the constant to check 必要參數(shù)。指定常量對(duì)象的名稱 |
Example
案例
<?phpdefine("GREETING","Hello you! How are you today?");echo defined("GREETING");?>
The output of the code above will be:
上述代碼將輸出下面的結(jié)果:
1
相關(guān)文章
php正則過濾html標(biāo)簽、空格、換行符的代碼(附說明)
最常用正則過濾代碼,能夠幫你過濾多余回車,注釋,html標(biāo)簽等。2010-10-10PHP mongodb操作類定義與用法示例【適合mongodb2.x和mongodb3.x】
這篇文章主要介紹了PHP mongodb操作類定義與用法,結(jié)合實(shí)例形式分析了php封裝的適合mongodb2.x和mongodb3.x版本MongoDB數(shù)據(jù)庫連接、增刪改查、錯(cuò)誤處理等操作定義與使用方法,需要的朋友可以參考下2018-06-06完美利用Yii2微信后臺(tái)開發(fā)的系列總結(jié)
Yii2是一個(gè)高性能,基于組件的 PHP 框架,這篇文章詳細(xì)的給大家介紹了利用Yii2開發(fā)微信后臺(tái)。我們一起來看看。2016-07-07php模擬服務(wù)器實(shí)現(xiàn)autoindex效果的方法
這篇文章主要介紹了php模擬服務(wù)器實(shí)現(xiàn)autoindex效果的方法,實(shí)例分析了php操作URL及傳遞參數(shù)的技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-03-03PHP自定義遞歸函數(shù)實(shí)現(xiàn)數(shù)組轉(zhuǎn)JSON功能【支持GBK編碼】
這篇文章主要介紹了PHP自定義遞歸函數(shù)實(shí)現(xiàn)數(shù)組轉(zhuǎn)JSON功能,針對(duì)json_encode函數(shù)處理GBK編碼中文出現(xiàn)亂碼的情況,使用自定義函數(shù)進(jìn)行數(shù)組遞歸遍歷實(shí)現(xiàn)可兼容GBK編碼的數(shù)組轉(zhuǎn)json功能,需要的朋友可以參考下2018-07-07PHP關(guān)于IE下的iframe跨域?qū)е聅ession丟失問題解決方法
一個(gè)登錄頁面,被別的網(wǎng)站用iframe嵌進(jìn)去后,死活無法登錄(只在IE中存在這種情況)。主要是session無法被保存的問題,下面把個(gè)人的解決過程分享個(gè)大家2013-10-10