未加星标

PHP: more array dereferencing weirdness

字体大小 | |
[开发(php) 所属分类 开发(php) | 发布者 店小二04 | 时间 2017 | 作者 红领巾 ] 0人收藏点击收藏

G'day:

(Firstly: I hasten to add that the "weirdness" is "weird to me ". It might be completely predictable to everyone else in thephp world).

This is a follow-on from my earlier article " PHP: accessing an undefined array element yields a notice. Or does it? WTF, PHP? ". Now before I start: Kalle Sommer Nielsen has come back to me in a comment on that article and mentions it's expected behaviour, and explained it... but I don't quite follow yet. I'm hoping some ensuing experimentation will cast some light on the scene.

My next variation of this has me scratching my head more. Well: it's demonstrating the same thing, but perhaps more clearly.

<?php echo PHP_EOL . "Test with array" . PHP_EOL; $a = [1, 2, 3]; var_dump($a[0]); var_dump($a[0]['id']); var_dump($a[0][0]['id']); var_dump($a[0]['id'][0]); echo PHP_EOL . "Test with int" . PHP_EOL; $i = 1; var_dump($i['id']); var_dump($i[0]['id']); var_dump($i['id'][0]); echo PHP_EOL; echo PHP_EOL . "Test with string" . PHP_EOL; $s = '1'; var_dump($s['id']); var_dump($s[0]['id']); var_dump($s['id'][0]);

This yields:

C:\temp>php test.php

Test with array

C:\temp\test.php:6:

int(1)

C:\temp\test.php:7:

NULL

C:\temp\test.php:8:

NULL

C:\temp\test.php:9:

NULL

Test with int

C:\temp\test.php:13:

NULL

C:\temp\test.php:14:

NULL

C:\temp\test.php:15:

NULL

Test with string

PHP Warning: Illegal string offset 'id' in C:\temp\test.php on line 21

PHP Stack trace:

PHP 1. {main}() C:\temp\test.php:0

Warning: Illegal string offset 'id' in C:\temp\test.php on line 21

Call Stack:

0.0002 356384 1. {main}() C:\temp\test.php:0

C:\temp\test.php:21:

string(1) "1"

PHP Warning: Illegal string offset 'id' in C:\temp\test.php on line 22

PHP Stack trace:

PHP 1. {main}() C:\temp\test.php:0

Warning: Illegal string offset 'id' in C:\temp\test.php on line 22

Call Stack:

0.0002 356384 1. {main}() C:\temp\test.php:0

C:\temp\test.php:22:

string(1) "1"

PHP Warning: Illegal string offset 'id' in C:\temp\test.php on line 23

PHP Stack trace:

PHP 1. {main}() C:\temp\test.php:0

Warning: Illegal string offset 'id' in C:\temp\test.php on line 23

Call Stack:

0.0002 356384 1. {main}() C:\temp\test.php:0

C:\temp\test.php:23:

string(1) "1"

C:\temp>

OK, so here I factor-out the top-level array from the mix. That test was always referring to the first element of the array , which is just the integer 1 , so we might as well just test with that . Needless to say, there's the same results as with the first array element . But I still don't get it. What is using array notation to dereference an integer supposedly doing? It's a non-sensical operation, as far as I understand things. And if it is a nonsensical operation: it should error.

For completeness I also try a variation with a string "1" . This does error like I'd expect . Well: it gives a warning and then returns an inappropriate (IMO) result anyhow. This is probably PHP doing it's combo of telling me I did it wrong but then going ahead and guessing what I meant anyhow. Grrr.

I'm gonna mess around with de-referencing integers some more when I get a moment, and try to work out WTF.

Righto.

--

Adam

本文开发(php)相关术语:php代码审计工具 php开发工程师 移动开发者大会 移动互联网开发 web开发工程师 软件开发流程 软件开发工程师

主题: PHP
分页:12
转载请注明
本文标题:PHP: more array dereferencing weirdness
本站链接:http://www.codesec.net/view/531808.html
分享请点击:


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 开发(php) | 评论(0) | 阅读(15)