assign data: array vs brackets, which is better?

advertisements

i'm just wondering the difference between this two, performance wise and which one is best to use

$data = array(
    'key1' => 'some value',
    'key2' => $some_variable
);

and this one

$data = array();  // can be omitted
$data['key1'] = 'some value';
$data['key2'] = $some_value


This comparison is not the way to think about. When you would be writing some practical code you will switch between both the methods alternatively depending upon needs of your problem. Like if you are accessing some row from a database you will have to use second one in a loop and although array() is rarely used but there are case when you will prefer it as string indexed array has to be assigned this way in a case where you are not getting values from $_POST or database. But there does exist cases when both methods can be used but you may prefer second one in a loop because your code will be shorter and you fingers have to bear less pain in assigning values to may be 10,000 entries. Happy Coding! :)