How to check whether an array is empty using PHP?


To check whether an array is empty, the code is as follows in PHP−

Example

 Live Demo

<?php
   $arr = array( "p"=>"150", "q"=>"100", "r"=>"120", "s"=>"110");
   if (empty($arr)) {
      echo "Empty Array...";
   }
   else{
      echo "Array isn't empty ...";
   }
?>

Output

This will produce the following output−

Array isn't empty ...

Example

Let us now see another example −

 Live Demo

<?php
   $arr = array( "p"=>"150", "q"=>"100", "r"=>"120", "s"=>"110");
   if (count($arr) == 0) {
      echo "Empty Array...";
   }
   else{
      echo "Array isn't empty ...";
   }
?>

Output

This will produce the following output−

Array isn't empty ...

Updated on: 26-Dec-2019

101 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements