Introduction to PHP short array syntax

Drupal is full of arrays, even in Drupal 8. But when you look at an array in Drupal 8, you may be left wondering why they look different to how they looked in Drupal 7. The reason is that the Drupal code standards now recommend using short array syntax.

Short array syntax was added to PHP in PHP 5.4 and replaces array() with [].

This is how it used to look with the long array syntax:

$array = array(
    "foo" => "bar",
    "bar" => "foo",
);

This is how it looks now with the newer shortened syntax:

$array = [
    "foo" => "bar",
    "bar" => "foo",
];

Both versions work in exactly the same way, the only difference is the code style.

More examples

The t() function

Drupal’s t() function allows you to translate a string. The second argument of the t() function is an array of replacements. Using long array syntax, the t() function with the replacements array would look like this:

t('Hello @name', array(’@name' => $name)

Using the short array syntax, it now looks like this:

t('Hello @name', ['@name' => $name])

Drupal form

Using long array syntax, the build method in a Drupal 8 form would look like this:

 /**
   * {@inheritdoc}
   */
  public function blockForm($form, \Drupal\Core\Form\FormStateInterface $form_state) {
    $form['welcome_message'] = array(
      '#type' => 'textarea',
      '#title' => t('Welcome message'),
      '#default_value' => $this->configuration['welcome_message'],
    );

    return $form;
  }

Using the short array syntax, it now looks like this:

  /**
   * {@inheritdoc}
   */
  public function blockForm($form, \Drupal\Core\Form\FormStateInterface $form_state) {
    $form['welcome_message'] = [
      '#type' => 'textarea',
      '#title' => t('Welcome message'),
      '#default_value' => $this->configuration['welcome_message'],
    ];

    return $form;
  }

Code standards

The Drupal code standards specify that:

  • There should be a space separating each array element
  • There should be spaces around the => (key association operator), if there is one
  • If the array spans more than 80 characters, break each element into its own line
  • If array elements are on their own line (above), indent by one level

For more information, check out the Drupal.org code standards for arrays.

Outro

Short array syntax was introduced in PHP 5.4 and was added to the Drupal code standards for Drupal 8. Short array syntax is cleaner and is consistent with other programming languages, such as Python. It is also easier to see closing array statement from the closing brackets of a function.

Comments

PHP short array syntax is great for most cases but just because a feature is available, does not necessarily mean we should use it.

This post is a clear example of that - https://matlipe.com/why-i-avoid-php-array-short-syntax/

Blair Wadman's picture

Thanks for the comment Duke. I totally agree that we shouldn't use features just because they are available.

However, in the case of Drupal, short array syntax are in the official code standards & developers should adhere to the code standards when writing Drupal code

Add new comment