Home Ask Login Register

Developers Planet

Your answer is one click away!

Kirmani88 February 2016

Whats is the difference between -> and [''] in php

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



Gordon February 2016

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:

Post Status

Asked in February 2016
Viewed 3,308 times
Voted 9
Answered 1 times


Leave an answer

Quote of the day: live life