PHP STRING 陷阱原理說明
更新時(shí)間:2010年07月24日 01:14:22 作者:
需要注意的時(shí)候,我們?cè)L問數(shù)組的時(shí)候 都是使用方括號(hào)“[]”,string作為一個(gè)也可以使用操作符“[]”進(jìn)行訪問。但是,需要注意的一點(diǎn)就是,訪問字符串時(shí)候,操作符“[]”中的內(nèi)容會(huì)被轉(zhuǎn)化為int類型的。
A string is series of characters.
String access and modification by character
Characters within strings may be accessed and modified by specifying the zero-based offset of the desired character after the string using square array brackets, as in $str[42]. Think of a string as an array of characters for this purpose.
Note: Strings may also be accessed using braces, as in $str{42}, for the same purpose. However, this syntax is deprecated as of PHP 5.3.0. Use square brackets instead, such as $str[42].
Warning
Writing to an out of range offset pads the string with spaces. Non-integer types are converted to integer. Illegal offset type emits E_NOTICE. Negative offset emits E_NOTICE in write but reads empty string. Only the first character of an assigned string is used. Assigning empty string assigns NUL byte.
以上都是 php manual 中的原話。
需要注意的時(shí)候,我們?cè)L問數(shù)組的時(shí)候 都是使用方括號(hào)“[]”,string作為一個(gè)也可以使用操作符“[]”進(jìn)行訪問。但是,需要注意的一點(diǎn)就是,訪問字符串時(shí)候,操作符“[]”中的內(nèi)容會(huì)被轉(zhuǎn)化為int類型的。
eg: $str ='123456';
echo $str['php'];//結(jié)果是1,因?yàn)閛ffset ‘php'轉(zhuǎn)化為integer為0,既是訪問的是字符串的第一個(gè)字符.
var_dump(isset($str['php']));//結(jié)果是bool(true) 原理同上。
所以,在我們使用isset判斷一個(gè)設(shè)置是否存在某個(gè)鍵時(shí)候,應(yīng)該先判斷試下,傳遞過來的變量是否是數(shù)組,然后再判斷是否是存在指定的key
eg://如果需要判斷傳遞過來的數(shù)組是否存在'php'這個(gè)key時(shí)候,比較安全的做法為:
function is_set($arr, $key){
if (is_array($arr) && isset($arr[$key])) {
//存在該值的邏輯
} else{
//$arr不是數(shù)組 或者 數(shù)組$arr不存在key $key的邏輯
}
}
如果 上面的函數(shù) 沒有添加 is_array 的判斷,當(dāng)傳遞一個(gè) 字符串過來的時(shí)候, 結(jié)果就不是我們預(yù)想的那樣了。
僅此為記,以免以后也出現(xiàn)類似的問題。
String access and modification by character
Characters within strings may be accessed and modified by specifying the zero-based offset of the desired character after the string using square array brackets, as in $str[42]. Think of a string as an array of characters for this purpose.
Note: Strings may also be accessed using braces, as in $str{42}, for the same purpose. However, this syntax is deprecated as of PHP 5.3.0. Use square brackets instead, such as $str[42].
Warning
Writing to an out of range offset pads the string with spaces. Non-integer types are converted to integer. Illegal offset type emits E_NOTICE. Negative offset emits E_NOTICE in write but reads empty string. Only the first character of an assigned string is used. Assigning empty string assigns NUL byte.
以上都是 php manual 中的原話。
需要注意的時(shí)候,我們?cè)L問數(shù)組的時(shí)候 都是使用方括號(hào)“[]”,string作為一個(gè)也可以使用操作符“[]”進(jìn)行訪問。但是,需要注意的一點(diǎn)就是,訪問字符串時(shí)候,操作符“[]”中的內(nèi)容會(huì)被轉(zhuǎn)化為int類型的。
eg: $str ='123456';
echo $str['php'];//結(jié)果是1,因?yàn)閛ffset ‘php'轉(zhuǎn)化為integer為0,既是訪問的是字符串的第一個(gè)字符.
var_dump(isset($str['php']));//結(jié)果是bool(true) 原理同上。
所以,在我們使用isset判斷一個(gè)設(shè)置是否存在某個(gè)鍵時(shí)候,應(yīng)該先判斷試下,傳遞過來的變量是否是數(shù)組,然后再判斷是否是存在指定的key
eg://如果需要判斷傳遞過來的數(shù)組是否存在'php'這個(gè)key時(shí)候,比較安全的做法為:
復(fù)制代碼 代碼如下:
function is_set($arr, $key){
if (is_array($arr) && isset($arr[$key])) {
//存在該值的邏輯
} else{
//$arr不是數(shù)組 或者 數(shù)組$arr不存在key $key的邏輯
}
}
如果 上面的函數(shù) 沒有添加 is_array 的判斷,當(dāng)傳遞一個(gè) 字符串過來的時(shí)候, 結(jié)果就不是我們預(yù)想的那樣了。
僅此為記,以免以后也出現(xiàn)類似的問題。
您可能感興趣的文章:
- 淺析php中常量,變量的作用域和生存周期
- 關(guān)于PHP5 Session生命周期介紹
- 深入理解PHP之OpCode原理詳解
- PHP的運(yùn)行機(jī)制與原理(底層)
- PHP內(nèi)核探索:哈希表碰撞攻擊原理
- PHP中的插件機(jī)制原理和實(shí)例
- 深入理解PHP原理之錯(cuò)誤抑制與內(nèi)嵌HTML分析
- 深入理解PHP原理之Session Gc的一個(gè)小概率Notice
- 理解php原理的opcodes(操作碼)
- 深入理解PHP原理之異常機(jī)制
- PHP原理之異常機(jī)制深入分析
- 修改Zend引擎實(shí)現(xiàn)PHP源碼加密的原理及實(shí)踐
- 深入理解PHP原理之執(zhí)行周期分析
相關(guān)文章
Zend Studio (eclipse)使用速度優(yōu)化方法
Zend studio7.12那速度正太讓人火大了,修改文件的保存就building workspace,要得等上好一會(huì)2011-03-03WordPress中重置文章循環(huán)的rewind_posts()函數(shù)講解
這篇文章主要介紹了WordPress中的文章循環(huán)重置函數(shù)rewind_posts()講解,附帶不依賴循環(huán)的single_cat_title()函數(shù)的用法介紹,需要的朋友可以參考下2016-01-01PHP使用CURL實(shí)現(xiàn)對(duì)帶有驗(yàn)證碼的網(wǎng)站進(jìn)行模擬登錄的方法
這篇文章主要介紹了PHP使用CURL實(shí)現(xiàn)對(duì)帶有驗(yàn)證碼的網(wǎng)站進(jìn)行模擬登錄的方法,可以幫助讀者加深對(duì)CURL操作的理解與應(yīng)用,需要的朋友可以參考下2014-07-07