PHP Difference between array() and []
When reviewing legacy codebases or migrating older PHP applications, you will frequently encounter the distinction between array('key' => 'value') and the modern shorthand ['key' => 'value']. While both syntaxes result in an array object being instantiated in memory, understanding when to use whichâand whyâmatters for maintainability and future-proofing your application.
The core functionality remains identical in terms of data structure. Whether you define a variable using array() or [], the resulting data type is a standard PHP array. If you pass either syntax into a function like form_input(), the internal logic processing that input will behave exactly the same, provided the function accepts an array argument. There is no hidden semantic difference in how the engine handles the data once it is created.
However, the decision to switch from array() to [] is rarely about technical capability; it is about version compatibility and project longevity. The short array syntax was introduced in PHP 5.4. Prior to that release, using square brackets for arrays resulted in a fatal error or a parse error depending on your configuration and PHP version.
If you are maintaining a legacy system running PHP 7.3, 8.0, or any version prior to 5.4 (which is highly unlikely today), sticking with array() ensures the code will not break. Conversely, if your project targets PHP 5.4 or newerâwhich includes all current Long Term Support (LTS) and Current versionsâ[] is the preferred approach. The primary reason to adopt the short syntax immediately is consistency. Modern PHP development relies heavily on tools like PSR-12 coding standards, which explicitly mandate the use of []. Mixing styles within a single codebase often leads to inconsistency that can confuse new developers joining the team or automated linters trying to enforce style guides.
Consider the specific case of mixing syntaxes in a file. You might see something like this:
<?php
// Legacy style
$config = array('timeout' => 30);
// Modern style
$userData = ['role' => 'admin', 'status' => 'active'];
// Using mixed styles in one expression (syntax error)
$invalid = array(['nested' => 'value']);
The last example actually works fine because array() is used for the outer container. However, developers often mistakenly try to use [] inside array() without realizing that while valid since PHP 5.4, mixing styles can still look messy and invite errors if you accidentally revert a PHP version upgrade later on.
The short array syntax also offers a slight ergonomic advantage when working with complex nested structures or unpacking arrays in function calls. While the original question focused on simple key-value pairs, the benefits scale up significantly as complexity increases. For instance, using [] makes it visually clearer when combining multiple arrays:
// Clearer intent with short syntax
$list = [...$existingItems, 'newItem' => true];
Regarding your second question about the short PHP tag <?= versus <?php echo ?>, this is a separate but related compatibility concern. The short echo tag was introduced in PHP 5.3. If you are running PHP 5.4 or later (which covers virtually all modern hosting environments), using <?= is perfectly safe and standard practice for simple output statements like echo form_input(...). It reduces character count and improves readability without sacrificing functionality.
The only scenario where <?= becomes problematic is if your application must support very old PHP versions or specific server configurations that have disabled short tags (e.g., via short_open_tag = Off in php.ini). In a modern Laravel or Symfony context, however, the default configuration enables these features, making both syntaxes safe to use.
Ultimately, unless you are explicitly required to support ancient PHP versions, migrating your codebase to [] and <?= is the responsible engineering choice. It aligns with current community standards, reduces the risk of parsing errors during future upgrades, and produces cleaner, more readable code.
