博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
php数组偏移,不能在php中使用字符串偏移作为数组
阅读量:6904 次
发布时间:2019-06-27

本文共 1017 字,大约阅读时间需要 3 分钟。

对于PHP4

…这转载的错误:

$foo = 'bar';

$foo[0] = 'bar';

对于PHP5

…这转载的错误:

$foo = 'bar';

if (is_array($foo['bar']))

echo 'bar-array';

if (is_array($foo['bar']['foo']))

echo 'bar-foo-array';

if (is_array($foo['bar']['foo']['bar']))

echo 'bar-foo-bar-array';

编辑,

so why doesn’t the error appear in the

first if condition even though it is a

string.

因为PHP是一个非常宽容的编程语言,我猜。我将用代码来说明我的想法:

$foo = 'bar';

// $foo is now equal to "bar"

$foo['bar'] = 'foo';

// $foo['bar'] doesn't exists - use first index instead (0)

// $foo['bar'] is equal to using $foo[0]

// $foo['bar'] points to a character so the string "foo" won't fit

// $foo['bar'] will instead be set to the first index

// of the string/array "foo", i.e 'f'

echo $foo['bar'];

// output will be "f"

echo $foo;

// output will be "far"

echo $foo['bar']['bar'];

// $foo['bar'][0] is equal calling to $foo['bar']['bar']

// $foo['bar'] points to a character

// characters can not be represented as an array,

// so we cannot reach anything at position 0 of a character

// --> fatal error

你可能感兴趣的文章
Delphi 常用控件之TlistView总结
查看>>
QUnit系列 -- 1.介绍单元测试(上)
查看>>
asp.net导出excel文件方法两则
查看>>
【103】若是有钱,我想买的东西
查看>>
single page
查看>>
Oil Deposits(poj 1526 DFS入门题)
查看>>
JavaEE入境后在做什么——公共入口疑问的答案
查看>>
Linux内核触摸屏驱动--多点触摸 【转】
查看>>
蓝桥杯——说好的进阶之入学考试
查看>>
开发API文档相关问题(*.chm)
查看>>
分布拟合——正态/拉普拉斯/对数高斯/瑞利 分布
查看>>
算法笔记_150:图论之双连通及桥的应用(Java)
查看>>
隐藏执行批处理bat文件
查看>>
函数y=sin(1/x)曲线
查看>>
WebStorm for Mac(Web 前端开发工具)破解版安装
查看>>
computational biology | Bioinformatician | 开发者指南
查看>>
Perl文件、目录常用操作
查看>>
从0开始--倒序输出。
查看>>
吉特仓库管理系统-.NET打印问题总结
查看>>
POJ 3026 Borg Maze(bfs+最小生成树)
查看>>