Check If array is null or not in php



PHP Snippet 1:

empty($result) 

PHP Snippet 2:

if (empty($result)) {
    return false;
}
if ( !($result['Tags'] instanceof SimpleXMLElement)) {
    return false;
}
return ($result['Tags']->count());

PHP Snippet 3:

if (!empty($result) {
    // do stuff if the variable is not empty
} else {
    // do stuff if the variable is empty
}

PHP Snippet 4:

if (is_null($result) {
   // do stuff if the variable is null
} else {
   // do stuff if the variable is not null
}

PHP Snippet 5:

/*
 return true if the array is not empty
 return false if it is empty
*/
function is_array_empty($arr){
  if(is_array($arr)){     
      foreach($arr as $key => $value){
          if(!empty($value) || $value != NULL || $value != ""){
              return true;
              break;//stop the process we have seen that at least 1 of the array has value so its not empty
          }
      }
      return false;
  }
}

PHP Snippet 6:

/* return true if values of array are empty
*/
function is_array_empty($arr){
   if(is_array($arr)){
      foreach($arr as $value){
         if(!empty($value)){
            return false;
         }
      }
   }
   return true;
}

PHP Snippet 7:

/*
 return true if the array is not empty
 return false if it is empty
*/
function is_array_empty($arr){
  if(is_array($arr)){     
      foreach($arr $key => $value){
          if(!empty($value) || $value != NULL || $value != ""){
              return true;
              break;//stop the process we have seen that at least 1 of the array has value so its not empty
          }
      }
      return false;
  }
}

if(is_array_empty($result['Tags'])){
    //array is not empty
}else{
    //array is empty
}

PHP Snippet 8:

if(implode(null,$arr)==null){
     //$arr is empty
}else{
     //$arr has some value rather than null
}