We hope you enjoy your visit.

You're currently viewing our forum as a guest. This means you are limited to certain areas of the board and there are some features you can't use. If you join our community, you'll be able to access member-only sections, and use many member-only features such as customizing your profile, sending personal messages, and voting in polls. Registration is simple, fast, and completely free.


Join our community!


If you're already a member please log in to your account to access all of our features:

Username:   Password:
Add Reply
PHP: Arrays and empty()
Topic Started: Mar 25 2005, 03:16 AM (771 Views)
FearKiller
Member Avatar
www.drewscripts.com
[ *  *  *  *  * ]
I made a script that posts data from page1 to page2. Page1 has four variables that are set by checkboxes.

Page2 has the following code on it to deal with these four variables in one array
Code:
 
<?php

$array = array("$var1", "$var2", "$var3", "$var4");

if (!empty($array))
{
echo ("The array is not empty");
}
else
{
echo ("The array is empty");
}

?>

Even if the user checks no boxes on page1, the array still shows up as not being empty on page2. If all the variables within the array are empty, then wouldn't the array itself be empty as well? What is in the array that is causing it to not be empty when none of the boxes are checked?
Offline Profile Quote Post Goto Top
 
Stefan
Member Avatar
Mew?
[ *  *  *  *  *  *  * ]
An array is empty when it contains no elements.
If it contains elements, it isn't empty, regardless of those elements being empty or not.
Offline Profile Quote Post Goto Top
 
FearKiller
Member Avatar
www.drewscripts.com
[ *  *  *  *  * ]
Oh, I see

Code:
 
<?php

$array = array("$var1", "$var2", "$var3", "$var4");

if (!empty($var1) || !empty($var2) || !empty($var3) || !empty($var4))
{
echo ("The array is not empty");
}
else
{
echo ("The array is empty");
}

?>


That'll work. Thanks for your help.
Offline Profile Quote Post Goto Top
 
Seth
Member Avatar
I has a pony
[ *  *  *  *  *  *  *  *  * ]
FearKiller
March 26, 2005 12:53 AM
Oh, I see

Code:
 
<?php

$array = array("$var1", "$var2", "$var3", "$var4");

if (!empty($var1) || !empty($var2) || !empty($var3) || !empty($var4))
{
echo ("The array is not empty");
}
else
{
echo ("The array is empty");
}

?>


That'll work. Thanks for your help.

Nooooooooooooooooooo!

Code:
 
<?php

$array = array($var1, $var2, $var3, $var4);
$stillEmpty = TRUE;

foreach ($array as $value) {
if (!empty($value)) {
$stillEmpty = FALSE;
break;
}
}

if ($stillEmpty) {
echo("Yarr I'm empty!");
} else {
echo("Yarr I'm full, yum!");
}
?>
Offline Profile Quote Post Goto Top
 
FearKiller
Member Avatar
www.drewscripts.com
[ *  *  *  *  * ]
Seth
March 26, 2005 02:39 AM
Nooooooooooooooooooo!

Code:
 
<?php

$array = array($var1, $var2, $var3, $var4);
$stillEmpty = TRUE;

foreach ($array as $value) {
if (!empty($value)) {
$stillEmpty = FALSE;
break;
}
}

if ($stillEmpty) {
echo("Yarr I'm empty!");
} else {
echo("Yarr I'm full, yum!");
}
?>

Hmm, that'll work to and looks much better, but shouldn't that last if statement read if($stillEmpty == "TRUE") rather than just if($stillEmpty) :unsure:
Offline Profile Quote Post Goto Top
 
Rory
i;m a mess
[ *  *  *  *  *  *  * ]
nope.

if($stillEmpty) checks whether $stillEmpty is TRUE or FALSE. If you want the condition to do something if false, then just do if(!$stillEmpty)

You could do

if($stillEmpty == TRUE)

but the first method is easier.
Offline Profile Quote Post Goto Top
 
1 user reading this topic (1 Guest and 0 Anonymous)
« Previous Topic · Technology Chat · Next Topic »
Add Reply