探討php define()函數(shù)及defined()函數(shù)使用詳解
The define() function defines a constant.
define()函數(shù)的作用是:定義一個常量。
Constants are much like variables, except for the following differences:
常量[constant]與變量[variable]有很多相似的地方,因此,很容易混淆;下面,我們列舉一下常量[constant]與變量[variable]之間的不同點:
A constant's value cannot be changed after it is set
一個常量值在指定之后就不可以更改;
Constant names do not need a leading dollar sign ($)
設(shè)置常量時,不需要在前面加上“$”符號;
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ū)分字母大小寫。默認值是:False |
Example 1
案例1
Define a case-sensitive constant:
指定一個常量(區(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:
指定一個常量(不區(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ù)的作用是:檢查一個常量是否存在。
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ù)。指定常量對象的名稱 |
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 mongodb操作類定義與用法示例【適合mongodb2.x和mongodb3.x】
這篇文章主要介紹了PHP mongodb操作類定義與用法,結(jié)合實例形式分析了php封裝的適合mongodb2.x和mongodb3.x版本MongoDB數(shù)據(jù)庫連接、增刪改查、錯誤處理等操作定義與使用方法,需要的朋友可以參考下2018-06-06php模擬服務(wù)器實現(xiàn)autoindex效果的方法
這篇文章主要介紹了php模擬服務(wù)器實現(xiàn)autoindex效果的方法,實例分析了php操作URL及傳遞參數(shù)的技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-03-03PHP自定義遞歸函數(shù)實現(xiàn)數(shù)組轉(zhuǎn)JSON功能【支持GBK編碼】
這篇文章主要介紹了PHP自定義遞歸函數(shù)實現(xiàn)數(shù)組轉(zhuǎn)JSON功能,針對json_encode函數(shù)處理GBK編碼中文出現(xiàn)亂碼的情況,使用自定義函數(shù)進行數(shù)組遞歸遍歷實現(xiàn)可兼容GBK編碼的數(shù)組轉(zhuǎn)json功能,需要的朋友可以參考下2018-07-07PHP關(guān)于IE下的iframe跨域?qū)е聅ession丟失問題解決方法
一個登錄頁面,被別的網(wǎng)站用iframe嵌進去后,死活無法登錄(只在IE中存在這種情況)。主要是session無法被保存的問題,下面把個人的解決過程分享個大家2013-10-10