Home  >  Article  >  Backend Development  >  Arrays and strings in php

Arrays and strings in php

巴扎黑
巴扎黑Original
2016-11-29 11:34:05885browse

Because PHP’s grammatical requirements are not strict, strings can also be used as arrays, so there is a problem: when using non-numbers as keys to access the content in the string, some inconsistencies may occur, such as the following Code

1 $hello = "hello" ;

2 var_dump( $hello [ 'abc' ]);

3 var_dump( $hello [ '0' ]);

4 var_dump( $hello [ '1abc' ]);

5 var_dump($hello ['12abc']);

Result:

'h'

'h'

'e'

''

I won't say the output result , you can know by simply running it. I think the reason is caused by intval. Due to time constraints, I did not confirm the zend code, but the running result page of the following code illustrates some problems

1 var_dump( intval ( 'abc ' ));

2 var_dump( intval ( '0' ));

3 var_dump( intval ( '1abc' ));

4 var_dump( intval ( '12abc' ));

This stuff, yes This is a very lethal bug during code review or testing. If the return value of a function is not well designed, sometimes it returns an array and sometimes it returns a string. Before using it, you must first determine whether the return result is an array. Otherwise, this bug will be blamed


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn