Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 433 Vote(s) - 3.64 Average
  • 1
  • 2
  • 3
  • 4
  • 5
PHP arrays... What is/are the meaning(s) of an empty bracket?

#1
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';`

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.
Reply

#2
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.
Reply

#3
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
Reply

#4
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'`.
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through