1

On the PHP documentation page for arrays, the example code given is the following:

<?php
$array = array(
    "foo" => "bar",
    "bar" => "foo",
);

// as of PHP 5.4
$array = [
    "foo" => "bar",
    "bar" => "foo",
];
?>

Does that mean that the first code is invalid in PHP 5.4, or is the second code just an alternative?

2

2 Answers 2

2

The first code is still valid. The second notation is just a shorter and more convenient alternative.

Sign up to request clarification or add additional context in comments.

Comments

0

As others noted the first one is valid. The second one is shorter version.

Here are the pros and cons for second version:

Pro

  Good for framework development when dealing with long parameterlists
  Other web languages have similar syntax
  Readable

Contra

  Yet another alias
  Would take distinctness from []
  Not searchable through search engines
  Unreadable
  Patch may be difficult to maintain in future

from PHPWiki

1 Comment

I want to smack whoever said the second version is "unreadable" in the head.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.