PHP arrays… What is/are the meaning(s) of an empty bracket?

拟墨画扇 提交于 2019-12-04 09:21:35

When you say $form['actions']['#type'] = 'actions', it assigns a value to $form['actions']['#type'], but when you say $form['#submit'][] = 'annotate_admin_settings_submit', if $form['#submit'] is an array, it appends 'annotate_admin_settings_submit' to the end of it, and if it's empty, it will be an array with one single element that is 'annotate_admin_settings_submit'.

The empty brackets mean that when the string is added to the array, php will automatically generate a key for the entry instead of it being specified in the brackets when populating the array. So $form['#submit'][] = 'annotate_admin_settings_submit'; is the same thing as $form['#submit'][0] = 'annotate_admin_settings_submit'; if it's the first time you do it. Next time it will be $form['#submit'][1] = 'annotate_admin_settings_submit';, etc.

Logamurugan Pilavadi Santhanam

The empty bracket adds an auto increment index to an array. The new index will be +1 to the last index.

Please check this example.

$form['#submit'][0] = 'zero';
$form['#submit'][1] = 'One';
$form['#submit'][] = 'Two'; // this will be considered as $form['#submit'][2] = 'Two';
$form['#submit'][4] = 'Four';
$form['#submit'][] = 'Four'; //this will be considered as $form['#submit'][5] = 'Four'; since its adds 4(last index)+1
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!