In php, there are three ways you can create an empty array. Creating an empty array can be useful if your data is dynamic.
$emptyArray = ; $emptyArray = array(); $emptyArray = (array) null
When working with arrays and collections of data in php, it is useful to be able to create and use different data structures easily.
We can easily create an empty array in php.
There are three ways you can use to define an empty array in php.
$emptyArray = ;
The second way to define an empty array in php is with the array() function.
$emptyArray = array();
The third and final way to create an empty array in php is by casting “null” to type array.
$emptyArray = (array) null;
In our opinion, the easiest way to initialize an empty array with no elements is the first way.
How to Add Elements to an Empty Array in php
After initializing an empty array, adding elements is easy. We can add items to an array with the php array_push() function.
Below is a simple example in php of creating an empty array and adding three elements to the array.
$emptyArray = ; print_r($emptyArray); array_push($emptyArray,"bear","snake","horse"); print_r($emptyArray); //Output: Array ( ) Array (  => bear  => snake  => horse )
Hopefully this article has been useful for you to learn how to create an empty array in your php programs.
Leave a Reply