Two ways to declare an array in PHP

An array is a data structure widely used in almost all programming languages, including PHP. An array contains a group of elements, and every element has an index or key. An array can be one-dimensional or multidimensional. Today, we will learn two ways to declare an array in PHP. I prefer the second one because it helps me to read the code easily.

First Method

This is the conventional method of declaring an array in PHP.

// First way

$cars = array("audi", "volvo", "mazda", "toyota", "BMW");
print_r($cars);

Second Method

This is the short-hand version of declaring an array in PHP. This syntax was introduced in PHP 5.4.0

// second way

$cars = ["audi", "volvo", "mazda", "toyota", "BMW"];
print_r($cars);
Total 0 Votes
0

Tell us how can we improve this post?

+ = Verify Human or Spambot ?

Leave a Comment

Back To Top