PHP: Check if variable is type of string AND is not empty string?
09:08 05 Nov 2015

I need to check if the passed variable is type of string, and it is not empty. I have the following function:

function isNonEmptyStr($var)
{
    if(isset($var)) {
        if(is_string($var)) {
            if(strlen($var) > 0) {
                return true;
            }
        }
    }
    return false;
}

results I expect:

echo(isNonEmptyStr(''));// false
echo(isNonEmptyStr(' '));// true
echo(isNonEmptyStr('a'));// true
echo(isNonEmptyStr('1'));// true
echo(isNonEmptyStr(1));// false
echo(isNonEmptyStr(0));// false
echo(isNonEmptyStr(0.0));// false
echo(isNonEmptyStr(0.1));// false
echo(isNonEmptyStr(array()));// false
echo(isNonEmptyStr(new myObj()));// false
echo(isNonEmptyStr(true));// false
echo(isNonEmptyStr(false));// false
echo(isNonEmptyStr(null));// false

The function works fine.

My question: Is there a way to improve function performance without effecting the results?

I'm talking about "micro optimization" (I use this function very heavily).

EDIT:

For those who are asking:

echo(isNonEmptyStr(0));// should return false, because it's not a string
echo(isNonEmptyStr(1));// should return false, because it's not a string
echo(isNonEmptyStr('0'));// should return true, because it's a non-empty string
echo(isNonEmptyStr('1'));// should return true, because it's a non-empty string

Note: a non-empty string = a string which if tested with strlen() function it would return > 0

php performance micro-optimization