Thanks to visit codestin.com
Credit goes to www.tutorialspoint.com

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: 2019-12-26T10:34:39+05:30

182 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements