Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 896
evilReiko Avatar asked Nov 05 '15 13:11

evilReiko


People also ask

How check variable is not NULL in PHP?

The is_null() function checks whether a variable is NULL or not. This function returns true (1) if the variable is NULL, otherwise it returns false/nothing.

How do you check if a string is non empty?

Java String isEmpty() Method The isEmpty() method checks whether a string is empty or not. This method returns true if the string is empty (length() is 0), and false if not.

How do you check if a variable is a string in PHP?

The is_string() function checks whether a variable is of type string or not. This function returns true (1) if the variable is of type string, otherwise it returns false/nothing.

Is empty string NULL in PHP?

NULL and empty - PHP Tutorial Null is a fancy term for nothing, for not having a value. It's not zero, it's not an empty string, it's the actual lack of a value.


1 Answers

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
like image 191
Flosculus Avatar answered Nov 07 '22 12:11

Flosculus