中文字幕日韩一区二区_国产一区二区av_国产毛片av_久久久久国产一区_色婷婷电影_国产一区二区精品

PHP STRING 陷阱原理說明

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 中的原話。
需要注意的時候,我們訪問數組的時候 都是使用方括號“[]”,string作為一個也可以使用操作符“[]”進行訪問。但是,需要注意的一點就是,訪問字符串時候,操作符“[]”中的內容會被轉化為int類型的。
eg: $str ='123456';
echo $str['php'];//結果是1,因為offset ‘php'轉化為integer為0,既是訪問的是字符串的第一個字符.
var_dump(isset($str['php']));//結果是bool(true) 原理同上。
所以,在我們使用isset判斷一個設置是否存在某個鍵時候,應該先判斷試下,傳遞過來的變量是否是數組,然后再判斷是否是存在指定的key
eg://如果需要判斷傳遞過來的數組是否存在'php'這個key時候,比較安全的做法為:
復制代碼 代碼如下:
function is_set($arr, $key){
if (is_array($arr) && isset($arr[$key])) {
//存在該值的邏輯
} else{
//$arr不是數組 或者 數組$arr不存在key $key的邏輯
}
}

如果 上面的函數 沒有添加 is_array 的判斷,當傳遞一個 字符串過來的時候, 結果就不是我們預想的那樣了。

僅此為記,以免以后也出現類似的問題。

php技術PHP STRING 陷阱原理說明,轉載需保留來源!

鄭重聲明:本文版權歸原作者所有,轉載文章僅為傳播更多信息之目的,如作者信息標記有誤,請第一時間聯系我們修改或刪除,多謝。

主站蜘蛛池模板: 婷婷久久网 | 操操网站 | 久久久精彩视频 | 欧美一区二区二区 | 久久夜夜 | 日韩精品一区二区三区久久 | 成人精品一区二区 | 成人网址在线观看 | 丝袜 亚洲 另类 欧美 综合 | 人人九九 | 日本黄色大片免费 | 精品在线观看入口 | 91久久婷婷 | 91精品久久久久久久久中文字幕 | 欧美成视频 | 9久久 | 久久久激情视频 | 国产网站在线播放 | 99视频在线看 | 天天想天天干 | 欧美日韩综合精品 | 国产欧美一区二区三区在线看 | 亚洲国产欧美国产综合一区 | 玖玖玖av| 在线免费观看毛片 | 久久99精品久久久久久秒播九色 | 日韩快播电影网 | 91一区二区| 凹凸日日摸日日碰夜夜 | 欧美国产一区二区三区 | 免费黄色a视频 | 国产美女在线观看 | 午夜理伦三级理论三级在线观看 | 欧美一区二区三区在线看 | 国产男人的天堂 | 久久69精品久久久久久久电影好 | www.99热这里只有精品 | 女同久久另类99精品国产 | 免费三级网站 | 国产精品综合 | 日本色综合 |