81

I have a question about PHP syntax.

When defining functions and variables, which convention should I use?

I know they do the same thing in practice but I would like to know which method conforms to best practice standards.

Variables

public $varname = array();

or

public $varname = [];

Methods

public function foo($bar = array()){}

or

public function foo($bar = []){}
4
  • 2
    Expect the code to be ran on php version < 5.4? use array() otherwise pick one and be consistent. There is no official standard. Commented Oct 30, 2014 at 11:40
  • 3
    That said, if you are using a framework (and you probably should be), then follow whatever practice the framework uses Commented Oct 30, 2014 at 11:42
  • Thanks, I am using cakphp 3.0 with some zend mixed in. CakePHP seems to be mixing it up a bit and have not investigated zend. Will never deploy on anything beneath 5.4. Think [] helps with legibility since it gives a distinction between method and array braces. Commented Oct 30, 2014 at 11:48
  • I prefer square brackets as well, as its consistent with many other languages. As i said, as long as you are consistent yourself you will be fine Commented Oct 30, 2014 at 11:53

5 Answers 5

97

PSR-2 and PSR-12 (by PHP Framework Interoperability Group) do not mention short array syntax. But as you can see in chapter 4.3 they use short array syntax to set the default value of $arg3 to be an empty array.

So for PHP >= 5.4 the short array syntax seems to be the de-facto standard. Only use array(), if you want your script to run on PHP <5.4.

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

Comments

34

from PHP docs:

As of PHP 5.4 you can also use the short array syntax, which replaces array() with [].

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

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

if you'd try using '[]' notation in earlier version than 5.4, it will fail, and PHP will throw a syntax error

for backward-compatibility you should use array() syntax.

3 Comments

Not downvoted, but i dont see how this answers the question at all
I think it answers the question, trying to use '[]' notation in a version prior to 5.4 is out of the question and is impossible.
The question asks "which convention should I use", and the answer is "you should use array() to preserve backward compatibility". How is it irrelevant?
10

just made a test to see how [] performs vs array() here is what I got

testing 1 million []s ( with $arr[] = [] )
started at : 1561298821.8754
ended at: 1561298822.6881
difference: 0.81266021728516 seconds 

testing 1 million array()s ( with array_push( $arr, array()) )
started at : 1561298822.6881
ended at: 1561298823.843
difference: 1.1549289226532 seconds 

testing 1 million []s ( again while the processor is hotter)
started at : 1561298823.8431
ended at: 1561298824.7448
difference: 0.9017219543457 seconds 

so [] performed about 20% faster

6 Comments

but you are performing a different operation (set to empty array vs array_push), so it's not apples to apples
$arr[] = [] and array_push( $arr, array()) performs the same operation, appends an empty array to an array (doesn't set array to an empty array) , the people who upvoted your post are most likely newbies
oh, now that you explain what it does, it actually makes sense, except it seems pointless appending an empty array as the next element
Upvoted OP's comment, due to fallacy of the first comment. Still I would argue it's the same apples being compared. Perhaps a better approach would be to compare $arr[] = [] and $arr[] = array(). And if you wish also (separately) $arr[] = [] and array_push( $arr, [] ).
Actually tested it and have dug into it. And indeed [] is a lot faster. And that's because [] is now what PHP deems as the primary operator. array() gets translated to [] first and then executed.
|
7

That depends on what version of PHP you are targeting. For the best backward compatibility, I'd recommend you to use array(). If you don't care about older versions (< PHP 5.4), I'd recommend you to use a shorter version.

Comments

1

it's obvious that new PHP projects should use [] because it's shorter, cleaner and similar to other languages like JS.

Comments

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.