Today, We want to share with you check array is empty or not in php.In this post we will show you PHP empty() Function, hear for Different ways of checking if an array is empty or not in PHP we will give you demo and example for implement.In this post, we will learn about checking array empty php with an example.
How to check whether an array is empty using PHP?
Syntax:
empty ($var); empty ($array);
if (empty($playerlist)) { // list is empty. }
PHP code:
<?php     $array1 = array('hello', 'world');     $array2 = array();         //checking whether arrays are empty or not     if (empty($array1)) {         echo "array1 is empty\n";     } else {         echo "array1 is not empty\n";     }         if (empty($array2)) {         echo "array2 is empty\n";     } else {         echo "array2 is not empty\n";     } ?>
Using empty() Function:
Example 1:
<?php $non_product_list_arr = array('URL' => 'https://www.pakainfo.org/'); $product_list_arr = array(); if(!empty($non_product_list_arr)) echo "Given Product Array is not empty <br>"; if(empty($product_list_arr)) echo "Given Product Array is empty"; ?>
Using the ‘empty’ function
<?php $my_arr = array('URL' => 'https://www.4cgandhi.com/'); $empty_arr = array(); if(!empty($my_arr)) echo "The array is non-empty <br>"; if(empty($empty_arr)) echo "The array is empty!"; ?>
Using count Function
Example 2:
<?php $product_list_arr = array(); if(count($product_list_arr) == 0) echo "Product Array is empty"; else echo "Product Array is non- empty"; ?>
Using sizeof() function
Example 3:
<?php $product_list_arr = array(); if( sizeof($product_list_arr) == 0 ) echo "Product Empty Array"; else echo "Product Non-Empty Array"; ?>
<?php $product_list_arr = array(); if( sizeof($product_list_arr) == 0 ) echo "The Product array is empty!"; else echo "The Product array is non-empty."; ?>
I hope you get an idea about how to check whether a given array in an empty array or not in PHP?.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.
I am Jaydeep Gondaliya , a software engineer, the founder and the person running Pakainfo. I’m a full-stack developer, entrepreneur and owner of Pakainfo.com. I live in India and I love to write tutorials and tips that can help to other artisan, a Passionate Blogger, who love to share the informative content on PHP, JavaScript, jQuery, Laravel, CodeIgniter, VueJS, AngularJS and Bootstrap from the early stage.