Better way to check variable for null or empty string?

// Function for basic field validation (present and neither empty nor only white space
function IsNullOrEmptyString($str){
    return (!isset($str) || trim($str) === '');
}

Old post but someone might need it as I did ;)

if (strlen($str) == 0){
do what ever
}

replace $str with your variable. NULL and "" both return 0 when using strlen.


Use PHP's empty() function. The following things are considered to be 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)

For more details check empty function

Tags:

Php

Validation