đź§ Is Your PHP Code Obsolete? Embrace [] for Empty Arrays
When starting a PHP project, you’ll often need to create an empty array. You might see either:
$list = array();
or
$list = [];
Both do the same thing—create an empty array—but which is better?
âś… The Basics
Let’s break it down:
$list
: A variable to hold data.=
: The assignment operator.[]
: Modern syntax for an empty array.array()
: Traditional function-based way to create an array.
🎯 So, What’s the Difference?
array()
is the old-school way. Been around since PHP 3.[]
is the modern shorthand, introduced in PHP 5.4.
Both work the same, but []
is cleaner and more current.
đź’ˇ Why You Should Use []
- Readability: Shorter and clearer.
- Modern Practice: Aligns with current PHP standards.
- Consistency: Keeps your codebase neat.
đź§Ş Quick Quiz
1. What’s the primary difference between $list = []
and $list = array()
?
(A) array()
creates a numeric array, []
creates an associative one
(B) array()
is deprecated
(C) Both are functionally the same, but []
is newer syntax âś…
(D) []
is faster than array()
2. Which syntax is recommended in modern PHP and why?
(A) array()
—it’s traditional
(B) []
—concise, modern, readable ✅
(C) Either—no performance difference
(D) array()
—more compatible with old versions
👨‍💻 Final Word
While both forms work, modern PHP developers prefer:
$list = [];
It’s cleaner, more readable, and fits today’s best practices. If you’re still using array()
, it’s time for an upgrade.