PHP STRING 陷阱原理說明
更新時間:2010年07月24日 01:14:22 作者:
需要注意的時候,我們訪問數(shù)組的時候 都是使用方括號“[]”,string作為一個也可以使用操作符“[]”進行訪問。但是,需要注意的一點就是,訪問字符串時候,操作符“[]”中的內(nèi)容會被轉化為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ù)組的時候 都是使用方括號“[]”,string作為一個也可以使用操作符“[]”進行訪問。但是,需要注意的一點就是,訪問字符串時候,操作符“[]”中的內(nèi)容會被轉化為int類型的。
eg: $str ='123456';
echo $str['php'];//結果是1,因為offset ‘php'轉化為integer為0,既是訪問的是字符串的第一個字符.
var_dump(isset($str['php']));//結果是bool(true) 原理同上。
所以,在我們使用isset判斷一個設置是否存在某個鍵時候,應該先判斷試下,傳遞過來的變量是否是數(shù)組,然后再判斷是否是存在指定的key
eg://如果需要判斷傳遞過來的數(shù)組是否存在'php'這個key時候,比較安全的做法為:
function is_set($arr, $key){
if (is_array($arr) && isset($arr[$key])) {
//存在該值的邏輯
} else{
//$arr不是數(shù)組 或者 數(shù)組$arr不存在key $key的邏輯
}
}
如果 上面的函數(shù) 沒有添加 is_array 的判斷,當傳遞一個 字符串過來的時候, 結果就不是我們預想的那樣了。
僅此為記,以免以后也出現(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ù)組的時候 都是使用方括號“[]”,string作為一個也可以使用操作符“[]”進行訪問。但是,需要注意的一點就是,訪問字符串時候,操作符“[]”中的內(nèi)容會被轉化為int類型的。
eg: $str ='123456';
echo $str['php'];//結果是1,因為offset ‘php'轉化為integer為0,既是訪問的是字符串的第一個字符.
var_dump(isset($str['php']));//結果是bool(true) 原理同上。
所以,在我們使用isset判斷一個設置是否存在某個鍵時候,應該先判斷試下,傳遞過來的變量是否是數(shù)組,然后再判斷是否是存在指定的key
eg://如果需要判斷傳遞過來的數(shù)組是否存在'php'這個key時候,比較安全的做法為:
復制代碼 代碼如下:
function is_set($arr, $key){
if (is_array($arr) && isset($arr[$key])) {
//存在該值的邏輯
} else{
//$arr不是數(shù)組 或者 數(shù)組$arr不存在key $key的邏輯
}
}
如果 上面的函數(shù) 沒有添加 is_array 的判斷,當傳遞一個 字符串過來的時候, 結果就不是我們預想的那樣了。
僅此為記,以免以后也出現(xiàn)類似的問題。
相關文章
Zend Studio (eclipse)使用速度優(yōu)化方法
Zend studio7.12那速度正太讓人火大了,修改文件的保存就building workspace,要得等上好一會2011-03-03WordPress中重置文章循環(huán)的rewind_posts()函數(shù)講解
這篇文章主要介紹了WordPress中的文章循環(huán)重置函數(shù)rewind_posts()講解,附帶不依賴循環(huán)的single_cat_title()函數(shù)的用法介紹,需要的朋友可以參考下2016-01-01PHP使用CURL實現(xiàn)對帶有驗證碼的網(wǎng)站進行模擬登錄的方法
這篇文章主要介紹了PHP使用CURL實現(xiàn)對帶有驗證碼的網(wǎng)站進行模擬登錄的方法,可以幫助讀者加深對CURL操作的理解與應用,需要的朋友可以參考下2014-07-07