DEV Community

Adeyeye George
Adeyeye George

Posted on

1

Difference between array() and []

Image description

In PHP, array() and [] are both used to create arrays, but they are slightly different.

array() is the traditional way to create an array in PHP, and it's a language construct. It can be used to create both indexed and associative arrays.

Example:

$fruits = array('apple', 'banana', 'orange');
Enter fullscreen mode Exit fullscreen mode

On the other hand, [] is a shorthand syntax for creating arrays, introduced in PHP 5.4. It's an alternative to array() and is used to create arrays in a more concise way.

$fruits = ['apple', 'banana', 'orange'];
Enter fullscreen mode Exit fullscreen mode

Both methods can be used interchangeably, but [] is generally preferred for its brevity and readability.

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay