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 to declare an array

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 to declare an array

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 1 Votes
0

Tell us how can we improve this post?

+ = Verify Human or Spambot ?

Leave a Comment

Back To Top
Cool Ajax - Web Development Tutorial Blog
Privacy Overview

This website uses cookies so that we can provide you with the best user experience possible. Cookie information is stored in your browser and performs functions such as recognising you when you return to our website and helping our team to understand which sections of the website you find most interesting and useful.