-2

I am checking a value to see if it is empty using the empty() function in PHP. This validates the following as empty:

"" (an empty string)
0 (0 as an integer)
0.0 (0 as a float)
"0" (0 as a string)
NULL
FALSE
array() (an empty array)
$var; (a variable declared, but without a value)

The value I am passing can be a string, array or number. However if a string has a space (" ") it is not considered empty. What is the easiest way to check this condition as well without creating my own function? I cannot just do an empty(trim($value)) since $value can be an array.

EDIT: I am not trying to ask how to check if a string is empty. I already know that. I am asking if there is a way that I can pass an array, number or string to empty() and it will return the correct validation even if the string passed has empty spaces in them.

Undefined Variable
  • 4,196
  • 10
  • 40
  • 69

3 Answers3

2

Just write an own isEmpty() function that fits your needs.

function isEmpty($value) {
    if(is_scalar($value) === false)
        throw new InvalidArgumentException('Please only provide scalar data to this function');

    if(is_array($value) === false) {
        return empty(trim($value));

    if(count($value) === 0)
        return true;

    foreach($value as $val) {
        if(isEmpty($val) === false)
            return false;
    }

    return false;
}
TiMESPLiNTER
  • 5,741
  • 2
  • 28
  • 64
  • Ya, this works, but the question specifically says `... without creating my own function ...` – Mark Miller Jun 27 '14 at 08:01
  • Thanks Timesplinter, but as Mark pointed out, I was checking the easiest way to do without my own function. – Undefined Variable Jun 27 '14 at 08:03
  • 1
    Well why should you not write your own function? You're a lot more flexible. You could parametrize everything. For example if the values should get trimmed for comparision against empty, etc. – TiMESPLiNTER Jun 27 '14 at 08:08
  • :-) You miss the point. I might end up writing my own function easily enough; I was just checking if there is any way to get it done easier that I wasnt aware of. Like the `ctype_space` option jurgemaister mentioned that I had totally forgotten about. BTW, your function is cool. – Undefined Variable Jun 27 '14 at 08:24
1

The best way is to create your own function, but if you really have a reason not to do it you can use something like this:

$original_string_or_array = array(); // The variable that you want to check
$trimed_string_or_array = is_array($original_string_or_array) ? $original_string_or_array : trim($original_string_or_array);
if(empty($trimed_string_or_array)) {
    echo 'The variable is empty';
} else {
    echo 'The variable is NOT empty';
} 
Samuil Banti
  • 1,735
  • 1
  • 15
  • 26
0

I really prefer the function TiMESPLiNTER made, but here is an alternative, without a function

if( empty( $value ) or ( !is_array( $value ) and empty( trim( $value ) ) ) ) {
    echo 'Empty!';
}
else {
    echo 'Not empty!';
}

Note that, for example $value = array( 'key' => '' ) will return Not empty!. Therefor, I'd suggest to use TiMESPLiNTERs function.

GreyRoofPigeon
  • 17,833
  • 4
  • 36
  • 59