Check Null In PHP

<?php
/**
 * 
 * @param $value the value that need to be checked
 * @return TRUE if value is not null/empty else FALSE
 */
function check_for_null($value) {
    if (is_array($value)) {
        if (count($value) > 0) {
            return TRUE;
        } else {
            return FALSE;
        }
    } else {
        if (($value != '') && ($value !== NULL) && (strlen(trim($value)) > 0)) {
            return TRUE;
        } else {
            return FALSE;
        }
    }
}

echo check_for_null(NULL); // return false
echo check_for_null(array()); // return false
echo check_for_null(10); // return true
echo check_for_null($apple); // return false

if ( check_for_null(10) ) {
    // not null do, something here
} else {
    // yes it is null, do something here
}

?>
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s