How to check whether an array is empty using PHP?

PHPServer Side ProgrammingProgramming

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 ...
raja
Published on 26-Dec-2019 10:34:39
Advertisements