Re: Testing if an array is empty



On Mon, 13 Aug 2007 17:32:33 +0200, zzapper <zzapper@xxxxxxxxx> wrote:
Have tried to google this without 100% satisfaction.

A function reads a mysql record into an array and returns it, if it
doesnt exist it returns '' or FALSE.

What ways do I have to test that my returned array is not empty

Something like the following?
<?php
if(empty($array)) echo '$array is empty';
?>

If you want an empty string, false, and an empty array handled seperately, you can use the following:

<?php
if($return===false){ //notice the triple '='
echo 'Function returned false';
} else if($return === ''){
echo 'Function returned empty string.';
} else if(is_array($return) && empty($return)){
echo 'Function returned empty array.';
} else {
echo 'Function returned:';
var_dump($return);
}
?>

--
Rik Wasmus
.



Relevant Pages

  • Re: Dynamic Form
    ... customer will answer all 5 questions then press next. ... I have read the PHP manual pages. ... echo Age; ... only other thought is that for some reason, I can't access the array ...
    (comp.lang.php)
  • Wordpress <=2.0.2 cache shell injection
    ... if user registration is enabled, ... php files. ... DB_PASSWORD costant could be empty, ... echo 'No response from '.$host.':'.$port; die; ...
    (Bugtraq)
  • Re: Macros in php
    ... From the PHP manual for empty: ... "The following things are considered to be empty: ... array() ... practice in PHP. ...
    (comp.lang.php)
  • Re: Passing session variables across pages
    ... I suppose internally PHP finds the key in the array *before* it calls the ... isset() function, so the E_NOTICE is raised before issetis called. ... echo "false\n"; ...
    (comp.lang.php)
  • Need help for the function ereg()
    ... Hi i'm a beginner in php and i would like to know why this code dosent ... echo $file_name; ... }//if download is choosen ... }//end if file name found in the array ...
    (comp.lang.php)