ovi.ro

ovi.ro

Strings array access issue

What would you expect for this script to output?

error_reporting(E_ALL); ini_set('display_errors',1);

$text = "abc";

var_dump(isset($text['key']));
var_dump((bool) $text['key']);

Maybe a notice? maybe some false there..
But if you have in mind PHP logic.. you surely know..

So, $text is a string and string characters can be accessed with $text[1] this is “b”
Then if you try $text[‘key’] PHP will typecast your ‘key’ to an integer (0) then $text[0] exists!

The second step is simple.. $text[0] is “a” and PHP logic again – any non empty string is true!

Output is:

bool(true);
bool(true);