0

I have been following 2 ways to get data from an array..sometimes i use

$username=      $data['username'];
$first_name=    $data['first_name'];

and sometimes,

$username=      $data->username;
$first_name=    $data->first_name;

Anytime i have an array, i know 1 of them will work, and it works..

but i am unable to understand what is the difference between them, I am never sure which one to use.

I google it a lot but unable to find any explanation.

Kindly guide me

Thanks

dev90
  • 6,447
  • 13
  • 62
  • 133

1 Answers1

8

That's a misconception.

The upper syntax with square brackets will only work for arrays (or objects implementing ArrayAccess).

The lower syntax using the T_OBJECT_OPERATOR will only work on objects. You cannot access arrays like that.

Compare the following links in the PHP manual:

Gordon
  • 305,248
  • 71
  • 524
  • 547