PHP STRING 陷阱原理说明


Posted in PHP onJuly 24, 2010

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 相关文章推荐
优化NFR之一 --MSSQL Hello Buffer Overflow
Oct 09 PHP
PHP4与PHP3中一个不兼容问题的解决方法
Oct 09 PHP
与数据库连接
Oct 09 PHP
PHP音乐采集(部分代码)
Feb 14 PHP
关于url地址传参数时字符串有回车造成页面脚本赋值失败的解决方法
Jun 28 PHP
PHP PDOStatement:bindParam插入数据错误问题分析
Nov 13 PHP
PHP设计模式之观察者模式(Observer)详细介绍和代码实例
Apr 08 PHP
PHP字符串word末字符实现大小写互换的方法
Nov 10 PHP
thinkPHP简单遍历数组方法分析
May 16 PHP
PHP如何读取由JavaScript设置的Cookie
Mar 22 PHP
浅谈php(codeigniter)安全性注意事项
Apr 06 PHP
gearman中worker常驻后台,导致MySQL server has gone away的解决方法
Feb 27 PHP
PHP下操作Linux消息队列完成进程间通信的方法
Jul 24 #PHP
php抓取页面与代码解析 推荐
Jul 23 #PHP
由php的call_user_func传reference引发的思考
Jul 23 #PHP
Google Voice 短信发送接口PHP开源版(2010.5更新)
Jul 22 #PHP
PHP 飞信好友免费短信API接口开源版
Jul 22 #PHP
PHP计划任务之关闭浏览器后仍然继续执行的函数
Jul 22 #PHP
PHP垃圾回收机制简单说明
Jul 22 #PHP
You might like
PHP的异常处理类Exception的使用及说明
2012/06/13 PHP
ThinkPHP3.2.3数据库设置新特性
2015/03/05 PHP
PHP实现动态添加XML中数据的方法
2018/03/30 PHP
laravel数据库查询结果自动转数组修改实例
2021/02/27 PHP
JavaScript之自定义类型
2012/05/04 Javascript
js编码、解码函数介绍及其使用示例
2013/09/05 Javascript
转换字符串为json对象的方法详解
2013/11/29 Javascript
JavaScript中的数组操作介绍
2014/12/30 Javascript
javascript中关于&& 和 || 表达式的小技巧分享
2015/04/10 Javascript
js+div实现文字滚动和图片切换效果代码
2015/08/27 Javascript
购物车前端开发(jQuery和bootstrap3)
2016/08/27 Javascript
JS常用函数和常用技巧小结
2016/10/15 Javascript
详解jquery插件jquery.viewport.js学习使用方法
2017/09/08 jQuery
详解关于Angular4 ng-zorro使用过程中遇到的问题
2018/12/05 Javascript
浅谈react-router@4.0 使用方法和源码分析
2019/06/04 Javascript
vue实现表格过滤功能
2019/09/27 Javascript
vue实现购物车小案例
2019/09/27 Javascript
Python入门篇之对象类型
2014/10/17 Python
Python使用matplotlib实现绘制自定义图形功能示例
2018/01/18 Python
用python 批量更改图像尺寸到统一大小的方法
2018/03/31 Python
python实现学员管理系统
2019/02/26 Python
Python简直是万能的,这5大主要用途你一定要知道!(推荐)
2019/04/03 Python
Python之Django自动实现html代码(下拉框,数据选择)
2020/03/13 Python
浅谈css3新单位vw、vh、vmin、vmax的使用详解
2017/12/01 HTML / CSS
深入探究HTML5的History API
2015/07/09 HTML / CSS
英国在线自行车商店:Evans Cycles
2016/09/26 全球购物
Lampenwelt德国:欧洲领先的灯具和照明在线商店
2018/08/05 全球购物
什么是Deployment descriptors;都有什么类型的部署描述符
2015/07/28 面试题
企业内部培训方案
2014/02/04 职场文书
目标责任书范文
2014/04/14 职场文书
酒店餐厅2014重阳节活动策划方案
2014/09/16 职场文书
股东出资证明书范例
2014/10/04 职场文书
工作检讨书怎么写
2015/01/23 职场文书
离婚协议书范本
2015/01/26 职场文书
会计求职自荐信范文
2015/03/04 职场文书
MySQL 计算连续登录天数
2022/05/11 MySQL