0

I have this code:

$myVariable = someRanDomFunction($blah)

The problem is that someRanDomFunction() might return an array, an object, an empty array, boolean value or NULL.

What if the best way to check that $myVariable has some data?

right now i'm doing this:

!empty($myVariable )

Will it cover all cases? or maybe i should do ($myVariable != NULL && !empty($myVariable ))?

UPDATE*

By 'some data' i mean TRUE if its bool, not empty array, and any value other than NULL

3
  • ...now first things first...when is the return value considered a 'failure'? Depending on the return value, you need to call a specific evaluation routine anyway, right? So why not check for is_array etc. manually, one after the other until you got it right? Commented Apr 8, 2013 at 20:30
  • 1
    Then !empty is exactly what you need ;) Commented Apr 8, 2013 at 20:36
  • Yes; empty. Nothing else. See my answer below what empty covers Commented Apr 8, 2013 at 20:41

6 Answers 6

2
var_dump(empty(NULL)); // returns bool(true)

empty is enough. If you say a boolean is no data, check also for !is_bool. (empty(false) returns also true)

Or better, use only if ($result); this should be enough.

Or what do you call "some data"?

UPDATE: What you need:

php > var_dump(!empty([]));

bool(false)

php > var_dump(!empty(""));

bool(false)

php > var_dump(!empty(false));

bool(false)

php > var_dump(!empty(true));

bool(true)

php > var_dump(!empty(null));

bool(false)

Sign up to request clarification or add additional context in comments.

Comments

1

Simply doing an implicit boolean check will exclude false, array(), and null.

if ($myVariable) {
    //will execute as long as myVariable isn't an empty array, false, null, or 0 / '0'
}

!empty() does exactly the same thing, with added suppression of warnings about undefined variables / array indices. It's a little wordier, though.

5 Comments

!empty('0') !== (bool)$myVariable
@bwoebi - what are you talking about?
[...] or 0 / '0' in your inline comment. (you said empty would do the exact same thing)
Those both evaluate to false.
arrgh... I confused something... When I'm too blind to open the eyes and see the "!" on my command line....
0

If you check for null, you need to use !== (strict checking http://php.net/manual/en/language.operators.comparison.php)

Comments

0

If your someRanDomFunction might return an array, object, bool or null this is bad way. Something wrong with logic.

However you might using an OR (||) operator, not AND.

($myVariable != NULL || !empty($myVariable) || $myVariable == false)

Comments

0

If you expect some specific data beforehand, it is better to check for it explicitly. PHP has a bunch of functions to do so they start with is_

Basically empty is checking for null, so your second way is redundant. You can also use isset

Comments

0

if the array, the object and boolean true is data AND The rest: an empty array, boolean false value or NULL, are not "data".

The solution then, would be this one:

$myVariable = someRanDomFunction($blah);

if(!empty($myVariable)) {
//...
}

1 Comment

What is "data" for you? << Thread opener had updated his post

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.