Viktor Iwan
Viktor Iwan

Reputation: 89

Weird Associative Array

At the moment I got very weird bug in PHP related with Associative Array, here the chunk of code:

$value="Hello World";
echo $value['randomassocname'];

Why it always return the first character which is "H" even though there's no assoc array attached ? how to fix this problem ?

Upvotes: 0

Views: 34

Answers (1)

Rizier123
Rizier123

Reputation: 59701

This is not a bug! If you have error reporting on:

<?php
    ini_set("display_errors", 1);
    error_reporting(E_ALL);
?> 

You would get this error:

Warning: Illegal string offset 'randomassocname'

So because you have that off you don't see the error and randomassocname gets implicit casted to an integer here 0 and it access the first character of the string

Upvotes: 2

Related Questions