Migrating from array() to Short Array Syntax [] in PHP

Migrating from array() to Short Array Syntax [] in PHP
Migrating from array() to Short Array Syntax [] in PHP

Learn how to migrate from the traditional array() to the modern short array syntax [] in PHP, enhancing code readability, consistency, and performance with best practices and strategies.

You open an old PHP file and it’s wall-to-wall array( ... ). Somewhere a linter or a teammate is nudging you to convert it all to []. Worth it? Here’s the straight answer: [] and array() build the exact same array. PHP just gave you a shorter way to write it back in 5.4, released in 2012. That’s the whole story. What follows is when to bother, how to do it without breaking anything, and which “benefits” are real versus repeated on the internet.

The Evolution of Arrays in PHP

array() is a language construct, not a function, and it has been in PHP about as long as arrays have. The short syntax [] arrived in PHP 5.4 to match how most other languages already wrote lists. Both produce identical arrays. Pick [] because it reads better, not because it does anything different.

Why Switch to Short Array Syntax?

Two reasons hold up. Two get oversold. Let’s be honest about which is which.

  • Readability
  • This is the real win. Dropping the word array and its parentheses cuts visual noise, and it adds up fast in nested structures.

    // Old syntax
    $colors = array('red', 'green', 'blue');
    // New syntax
    $colors = ['red', 'green', 'blue'];

    Fewer characters, fewer ways to fumble a paren. In a large file, your eyes thank you.

  • Consistency with other languages
  • JavaScript, Python, and Ruby all write lists with square brackets. If you jump between them all day, matching syntax is one less thing to context-switch on.

    // JavaScript array
    let colors = ['red', 'green', 'blue'];

    Small thing, but real when you’re full-stack.

  • Performance? No.
  • You’ll read that [] is faster because the parser does less work. It isn’t. Both forms compile to the same opcode (ZEND_INIT_ARRAY); at runtime they’re indistinguishable. Run the benchmark below yourself and you’ll get numbers that flip on every run, which is measurement noise, not a speedup.

    // Measuring performance
    $start = microtime(true);
    for ($i = 0; $i < 1000000; $i++) {
        $array = array('a', 'b', 'c');
    }
    $time = microtime(true) - $start;
    echo "Time using array(): $time\n";
    $start = microtime(true);
    for ($i = 0; $i < 1000000; $i++) {
        $array = ['a', 'b', 'c'];
    }
    $time = microtime(true) - $start;
    echo "Time using []: $time\n";

    Switch for readability. Don’t sell anyone on speed.

  • Future-proofing? Mostly a myth too.
  • array() is not deprecated, and there’s no plan to remove it. Your code won’t break next release for using it. The honest reason to standardize on [] is a consistent, modern-looking codebase, not fear of a deprecation that isn’t coming.


Transition Strategies: Migrating from array() to []

Because the two forms are identical, this migration is unusually safe. The swap can’t change behavior. The only real questions are how much you convert and how.

Manual refactoring

Fine for a handful of files. Go through, swap array() for [], and clean up anything ugly while you’re in there.

// Before
$fruits = array('apple', 'banana', 'cherry');
// After
$fruits = ['apple', 'banana', 'cherry'];
Automated tools

For anything larger, let a tool do it. Rector or PHP CS Fixer will convert the whole project in one pass, consistently, with none of the copy-paste mistakes you’d make by hand.

php-cs-fixer fix /path/to/your/project --rules=array_syntax

The same tools enforce your other standards too, so one config keeps future code in line.

Gradual refactoring

On a big, active codebase, don’t stop the world for this. Write [] in new code and convert old files as you touch them. Your codebase modernizes on its own, no dedicated migration project required.

Testing

Run your tests after, as always. But be clear-eyed: since [] and array() produce the same array, a clean syntax swap won’t change behavior. Tests here are cheap insurance against a botched find-and-replace, not a real behavioral risk.


Best Practices for Adopting Short Array Syntax

Keep it simple:

  • Pick one and enforce it. Add the array_syntax rule to PHP CS Fixer so new code is converted automatically. A machine is more consistent than a code review.
  • Convert legacy as you go. Prioritize the files you edit most; leave cold, working code alone until you have a reason to open it.
  • Wire it into CI. If the fixer runs in your pipeline, the standard holds itself without anyone remembering to care.

The bottom line

Migrating from array() to [] is a readability upgrade, not a performance or compatibility one. It’s a safe, boring change, which is exactly why you should automate it and move on. Let a tool convert what’s there, set the rule so new code stays clean, and spend the attention you saved on something that actually changes how your app runs.

Next: Transitioning from ereg to preg_match in PHP: Best Practices

2 thoughts on “Migrating from array() to Short Array Syntax [] in PHP”

Leave a Comment

Your email address will not be published. Required fields are marked *


Scroll to Top