PHP Empty Array | How to Check if Array is Empty PHP With Example
Summary: In this article, we learn PHP empty array and also learn how to check if array is empty PHP. this article is useful to php check for empty array. Let’s understand in details.
Definiation and Usages
Arrays in PHP: Use the array() function to create an array in PHP. Basically, three types of an array which supported in PHP:
- Indexed arrays: Arrays having a numeric index.
- Associative arrays: Arrays having named keys.
- Multidimensional arrays: It contains one or more arrays in a particular array.
The Best way to initialize empty array in PHP
it’s always the best execution or practice to declare an empty array and then push the items to that array?
When declaring in PHP an empty array and then beginning to enter elements in it later. With the support of this, it can prevent different errors due to a faulty array. It helps to have the information or details of using bugged, rather than having the array. It saves time throughout the debugging. furthermore, most of the time it may not have something to add to the array at the point of creation.
There are the syntax to create an PHP empty array:
$emptyArray = array();
$emptyArray = (array) null;
Further, While pushing an element to the array then it can use $emptyArray[] = "first"
. Therefore, at this time, $emptyArray contains “first”, with this command and it will send “first” to the array which is declared empty at beginning or starting.
also, we can say that in other words, the initialization of the new array is faster, use syntax var first = [] rather while using syntax var first = new Array(). Furthermore, the fact is being a constructor function the function Array() and the, [] is a part of the literal grammar of array. Therefore, both are complete and executed in completely different methods or ways. Here, both functions are optimized and not bothered by the overhead of any of the calling functions.
PHP Empty Array Example
Program 1: Basic Program
1 2 3 4 |
<?php $empty_Array = (array) null; var_dump($empty_Array); ?> |
empty
Program 2:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
<?php /* This method to create an Empty array. */ $firstempty = []; echo "It will Created the First empty array <br>"; /* This method to create the Second Empty array. */ $secondempty = array( ); echo "it will Created the second empty array<br>"; /* First method to create array. */ $first = array( 11, 12); foreach( $first as $value ) { echo "Value is $value <br>"; } /* Second method to create array. */ $first[0] = "eleven"; $first[1] = "twelve"; foreach( $first as $value ) { echo "Value is $value <br>"; } ?> |
Created the second empty array
The Value is 11
The Value is 12
Value is eleven
Value is twelve
Program 3: Used Another Method:
1 2 3 4 5 6 7 8 9 10 |
<?php // To Create an empty array $empty_Array=array(); // To Push elements to the array array_push($empty_Array, "welcome", "to", "tutorial", "scan"); // To Display array elements print_r($empty_Array); ?> |
Question 1: How to check whether an array is empty?
Answer: There are various methods and functions available in PHP to check whether the defined or given array is an empty or not. Some of them are given below:
1) Using count Function
2) Using empty() Function
3) Using sizeof() function
Ist Method: Using count Function
This count() function in PHP counts all the elements in an array. Furthermore, if the number of elements in the array is 0 (zero), then it will always display an empty array.
Example:
1 2 3 4 5 6 7 8 9 10 11 |
<?php // To Declare an empty array $emptyarray = array(); // Function to count array // element and use condition if(count($emptyarray) == 0) echo "The array is empty"; else echo "The array is non- empty"; ?> |
2nd Method: Using empty() Function
This empty() function Identify whether a given variable is empty. This empty() function in PHP doesn’t return a warning if a variable doesn’t exist.
Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<?php // To Declare an array and initialize it $nonemptyarray = array('URL' => 'https://www.tutorialscan.com/'); // To Declare an empty array $emptyarray = array(); // This Condition to check array is empty or not if(!empty($nonemptyarray)) echo "The given an array is not empty <br>"; if(empty($emptyarray)) echo "The given an array is empty"; ?> |
The given an array is empty
3rd Method: Using sizeof() function
This sizeof() function, checks the size of the array. If the size of the array is 0 (zero) then the array is empty otherwise array isn’t empty.
Example:
1 2 3 4 5 6 7 8 9 10 11 |
<?php // To Declare an empty array $emptyarray = array(); // Use array index to check // array is empty or not if( sizeof($emptyarray) == 0 ) echo "An empty Array"; else echo "Non-Empty Array"; ?> |
Conclusion:
In this article, you have learned PHP empty array and also how to check if array is empty PHP. I hope you will enjoy it!. if have any query related program then contact me at info@tutorialscan.com I will try to resolve the problem.
Stay healthy, stay safe! Happy coding! enjoy it!