How to create an empty array in PHP? Explain with example?

Posted by

array() function is use to create an array in our PHP and php supports three types of array-

  • index array= it has index values
  • associative array= it has string keys
  • multidimensional array= it contains one or more array in particular array

 Why it is always good practice to declare an empty array and then push the items to that array?

when declare an empty array and then start entering elements in it later. with the help of this, it can prevent different errors due to a faulty array. it helps to have the information of using bugged, rather having the array. it saves time during the debugging. Most of the time it may not have anything to add to the array at the point of creation.

Syntax
$emptyArray = [];
      or
$emptyArray = array();

       or
$emptyArray = (array) null;
EMPTY ARRAY
example
<?php

		$subjects = array();
		if (count($subjects) == 0)
		{
				echo " the array is empty";
		}
		else
		{
		    echo "array is not empty";
		}
		
?>

output

the array is empty
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x