tags:

views:

242

answers:

7

How can i test a var if its empty or not? i have this array

array($myname,$myhouse,$mywife);

how can i check each of them if it empty or not without using a loop?

+2  A: 

Try isset()

Edit- Use the code posted above, but change the function:

$myvars = array($myname, $myhouse, $mywife);
foreach ($myvars as $value) {
 if (isset($value)) {
  // Not set
 }
}

According to this, isset is marginally faster.

Matt
Yeah but howto check eachone if is isset?
streetparade
You need to loop through them.
Matt
+3  A: 
$myvars = array($myname, $myhouse, $mywife);
foreach ($myvars as $value) {
  if (empty($value)) {
    // value is empty
  }
}

edit: Here is a version which does not use a loop:

$myvars = array($myname, $myhouse, $mywife);
$myvars_filtered = array_filter($myvars, "empty"); // or isset, if you prefer
if (count($myvars_filtered) > 0) {
  // one or more values were empty
}

Note that, as Felix commented, when you use "isset" instead of "empty", you will get all values that have a value, instead of the ones that don't. So in that case you need to compare the amount of elements in $myvars to the count of elements in $myvars_filtered.

Aistina
Noop without loopp
streetparade
@streetparade: check each one separately? `if (empty($myname) || empty($myhouse) || empty($mywife)){...}`
FrustratedWithFormsDesigner
but that also to long version
streetparade
@streetparade: If you don't want to spell out each variable and you don't want a loop, you will have to use magic. Try `require "magic.php";`
FrustratedWithFormsDesigner
Be careful with interchanging `empty` and `isset`. When using `isset` you get all variables that are set and with `empty` all that are **not** set/empty. Apart from that, nice solution *THUMBS UP* :)
Felix Kling
Oops Felix, you're right. I'll add that to my answer.
Aistina
+8  A: 

What you're asking for is either impossible or facetious. You either need to explicitly test each variable by name or use a loop to test a dynamic collection of variables.

Eric Kolb
There are some ways… wondering why this answer has already 5 upvotes.
Nils Riedemann
And now even accepted? Weird. There are already some clever answers.
Nils Riedemann
Even the clever answers have loops, but they're hidden from the developer. Perhaps this was an assignment/homework question?
FrustratedWithFormsDesigner
@FrustratedWithFormsDesigner is correct -- every method you could propose to do this requires a loop or an explicit listing. Array_map loops over all the items in an array. Concatenating values together can't be done without iterating over the contents of the array.Just because you didn't write 'foreach' doesn't mean it's not a loop.
Eric Kolb
Yeah it's silly. Not *writing* a loop doesn't mean you aren't using one. The only other option is checking the values one by one hardcoding them
kemp
+7  A: 

One line (except the function of course):

function not_empty($val) {
    return !empty($val);
}

$result = array_product(array_map('not_empty',$array));

The result is 0 if one variable is empty otherwise 1.
But of course it won't tell which one is empty. ;)

See array_map, array_product.

Felix Kling
Oooh this is clever! ...but of course, using mapping functions implies that there is still some looping going on behind the scenes.
FrustratedWithFormsDesigner
Of course looping (at some point) cannot be avoided...
Felix Kling
this is really really clever! Nice one.
Nils Riedemann
+5  A: 

If you mean without writing a loop then you can use:

in_array(true, array_map(create_function('$x', 'return empty($x);'),$array));

Implicitly it will of course loop through your array, twice in fact! Once to check the emptiness, then again to check the result. You're better of with a loop you can break when you hit a non_empty value.

Martin
Oh that's also a good one! Still has lot of implicit loops though.
FrustratedWithFormsDesigner
+1  A: 

Another solution that doesnt use a loop:

<?php
$my_house = "";
$my_name = "Foo Bar";
$my_wife = "Angelina";

$arr = array($my_house, $my_name, $my_wife);
preg_replace("/.+/","-",$arr,-1,$count);
if ($count == 3) {
  # everything is filled
} else {
  # missing somehwere
}
?>
Nils Riedemann
+1  A: 

Sure there are ways to do it. One of them:

<?php
$array = array('myname' => 'Jakob', 'myhouse' => '', 'mywife' => 1);

$empty_elements = array("");
$array = array_keys(array_intersect($array,$empty_elements));

var_dump($array);
?>

outputs:

array 0 => string 'myhouse'

Some other possibilities for a similiar problem (removing the empty ones): http://hasin.wordpress.com/2009/09/16/removing-empty-elements-from-an-array-the-php-way/

Jakob Stoeck
In the link you can see why he presumably doesn't want a php-loop: It's way slower than using internal loops.
Jakob Stoeck