Search code examples
phpperformancemicro-optimization

PHP: Check if variable is type of string AND is not empty string?


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


Solution

  • Here is a simple little benchmarking script you can modify to see what works best. I just tried a few variations of the same thing, the first one is the fastest by a small margin, but they are basically all the same. And there isn't really a simpler way for you to write it.

    Also $val === '' is slightly faster than empty($val), on top of being more strictly correct for you.

    Additionally, since this is basically a one liner, why not just cut the overhead of making it a function and call is_string($val) && $val !== '' directly. It don't make a huge difference, but its noticeable for millions of iterations, but I doubt this procedure will be the main bottleneck in any of your code ever...

    function is_non_empty_string_1($val)
    {
        return is_string($val) && $val !== '';
    }
    
    function is_non_empty_string_2($val)
    {
        return gettype($val) === 'string' && $val !== '';
    }
    
    function is_non_empty_string_3($val)
    {
        switch (true) {
            case !is_string($val): return false;
            case $val === '': return false;
        }
    
        return true;
    }
    
    $values = array('', '1', new stdClass(), 1, 2, 3, 999, array(), array());
    $runs = 2000000;
    
    
    function benchmark($test, $values, $runs, $func)
    {
        $time = time();
        for ($i = 0; $i < $runs; $i++) {
            foreach ($values as $v) {
                $func($v);
            }
        }
        echo $test . '. ' . (time() - $time) . PHP_EOL;
    }
    
    benchmark(1, $values, $runs, 'is_non_empty_string_1');
    benchmark(2, $values, $runs, 'is_non_empty_string_2');
    benchmark(3, $values, $runs, 'is_non_empty_string_3');
    

    Results:

    1. 5
    2. 6
    3. 6