PHP arrays… What is/are the meaning(s) of an empty bracket?
Clash Royale CLAN TAG#URR8PPP
PHP arrays… What is/are the meaning(s) of an empty bracket?
I ran across some example code that looks like this:
$form['#submit'] = 'annotate_admin_settings_submit';
Why is there a bracket after ['#submit'] that is empty with nothing inside? What does this imply? Can anyone give me an example? Normally (from my understanding which is probably wrong) is that arrays have keys and in this case the the $form array key '#submit' is equal to 'annotate_admin_settings_submit' but what is the deal with the second set of brackets. I've seen examples where an array might look like:
$form['actions']['#type'] = 'actions';
$form['actions']['#type'] = 'actions';
I know this is a very basic question about php in general but I ran across this question while learning Drupal so hopefully someone in the Drupal community can clarify this question that I'm obsessing over.
That's still array appending. In this case pushes an entry to an subarray just.
– mario
Dec 20 '13 at 17:40
Oh so it is essentially placing 'annotate_admin_settings_submit' at the end of the $form array?
– Daniel Miladinovich
Dec 20 '13 at 17:43
Now that you know the meaning of
, note that adding such elements to #submit
means you ask Drupal to execute that function in addition to current ones. If you don't want to remove existing submit handlers but further process the form values, this is how you do it.– Ayesh K
Dec 21 '13 at 17:34
#submit
great thanks for the supplementary info Ayesh.
– Daniel Miladinovich
Dec 21 '13 at 18:58
3 Answers
3
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'
.
$form['actions']['#type'] = 'actions'
$form['actions']['#type']
$form['#submit'] = 'annotate_admin_settings_submit';
$form['#submit']
'annotate_admin_settings_submit'
'annotate_admin_settings_submit'
Thank you very much! This is what the others (mario and UnLoCo) said in the comments just below my question. I was hoping someone would turn it into an answer instead of a comment :). Thanks.
– Daniel Miladinovich
Dec 20 '13 at 17:48
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.
$form['#submit'] = 'annotate_admin_settings_submit';
$form['#submit'][0] = 'annotate_admin_settings_submit';
$form['#submit'][1] = 'annotate_admin_settings_submit';
Thank you very much.
– Daniel Miladinovich
Dec 20 '13 at 17:50
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
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
check php.net/array_push
– UnLoCo
Dec 20 '13 at 17:38