🐘 PHP Beginner

What is the difference between array() and [] syntax in PHP?

Answer

Both array() and [] create arrays in PHP — they are functionally identical. The [] short syntax was introduced in PHP 5.4 and is now universally preferred because it is more concise and reads like array literals in other languages. $fruits = ["apple", "banana"] is exactly equivalent to $fruits = array("apple", "banana"). The short syntax also works for destructuring assignments (PHP 7.1+): [$first, $second] = $array; as opposed to list($first, $second) = $array;. Modern PHP code almost exclusively uses the short [] syntax.