78

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 = []){}
Robbie JW
  • 729
  • 1
  • 9
  • 22
Dieter Gribnitz
  • 5,062
  • 2
  • 41
  • 38
  • 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. – Steve Oct 30 '14 at 11:40
  • 3
    That said, if you are using a framework (and you probably should be), then follow whatever practice the framework uses – Steve Oct 30 '14 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. – Dieter Gribnitz Oct 30 '14 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 – Steve Oct 30 '14 at 11:53

5 Answers5

95

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.

Movahhedi
  • 302
  • 4
  • 15
BreyndotEchse
  • 2,192
  • 14
  • 20
31

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.

Community
  • 1
  • 1
Nitsan Baleli
  • 5,393
  • 3
  • 30
  • 52
  • 3
    Not downvoted, but i dont see how this answers the question at all – Steve Oct 30 '14 at 11:41
  • 6
    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. – Nitsan Baleli Oct 30 '14 at 11:46
  • 8
    The question asks "which convention should I use", and the answer is "you should use array() to preserve backward compatibility". How is it irrelevant? – SOFe Feb 26 '18 at 09:03
9

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

Aurangzeb
  • 1,537
  • 13
  • 9
  • 6
    but you are performing a different operation (set to empty array vs array_push), so it's not apples to apples – My1 Nov 08 '19 at 20:49
  • 3
    $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 – Aurangzeb Dec 07 '20 at 15:02
  • oh, now that you explain what it does, it actually makes sense, except it seems pointless appending an empty array as the next element – My1 Dec 07 '20 at 23:11
  • 3
    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, [] )`. – s3c Jan 20 '21 at 15:45
  • 2
    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. – Alex Jul 30 '21 at 09:16
  • @Alex Could you explain where can I see proof that array() gets translated to [] so that I can present this reasoning to other people? – rosaqq Dec 10 '21 at 10:43
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.

Valentin Rodygin
  • 864
  • 5
  • 12
0

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

Andrei Nikolaenko
  • 1,022
  • 1
  • 7
  • 13