Jump to content

Recommended Posts

Hi everyone,

 

I am using an array named $outdata, and just wish to know how to check if the array is empty.

 

I am using this method  if(empty($outdata)); i am not sure this is the right way to check if array is empty.

 

 

Here is the code

 

$outdata=array();
$outdata[]="Hello";
$outdata[]="Welcome";

if(empty($outdata)); 
{
$outdata[]="You are not authorized";
}
else
{
do something...
}

Link to comment
https://forums.phpfreaks.com/topic/62669-how-to-check-if-array-is-empty/
Share on other sites

Nope!

 

use:

 

if(count($array) == 0) {

//The array is empty, it either doesn't exist or has no elements in it

} else {

The array exists and has things in it

}

 

 

or:

 

if(isset($array)) {

//The array exists, but might be empty

} else {

//The array does not exist at all, it was either never defined or unset/deleted

}

 

Depending on what exactly you want it to do. Choosing between them is what makes coding slightly more than following a recipe book.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.