
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Different Ways of Checking if an Array is Empty in PHP
Using the ‘sizeof’ function
Let us see an example −
Example
<?php $empty_arr = array(); if( sizeof($empty_arr) == 0 ) echo "The array is empty!"; else echo "The array is non-empty."; ?>
Output
The array is empty!
An array can be checked to see if it is empty or not in multiple ways. One method is to use the ‘sizeof’ function that sees if the array is empty. If yes, the size would be 0, thereby confirming the array being empty.
Using the ‘empty’ function
Example
<?php $my_arr = array('URL' => 'https://www.medium.com/'); $empty_arr = array(); if(!empty($my_arr)) echo "The array is non-empty <br>"; if(empty($empty_arr)) echo "The array is empty!"; ?>
Output
The array is non-empty The array is empty!
Another method to check if an array is empty or not is to use the ‘empty’ function, that checks to see the contents of the array, and if nothing is present, says it is empty.
Advertisements