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. If you have recently started PHP coding, then it is the most suitable option. Also, it provides a visual context of the variable declaration.
// First way $cars = array("audi", "volvo", "mazda", "toyota", "BMW"); print_r($cars);
✅ Second method to declare an array
This is the shorthand 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
0