What function approach should I use

A very simple question, but I wanted to get expert advice, so I post it here.

Here are two functions.
What is the difference between two? Are they both equivalent effective and include best practices or which one is best used in programming.

function is_numeric($number)
{
    if(!preg_match("/^[0-9]+$/",$number))
        return false;
    return true;
}

function is_numeric($number)
{
    if(preg_match("/^[0-9]+$/",$number))
        return true;
    else
        return false;
}
+5
source share
6 answers

Some coding standards indicate that the first branch should be more likely, while the else branch should handle more exceptional things.

But it is completely esoteric, choose what you want.

In my personal opinion, rather use

function is_numeric($number)
{
    return preg_match("/^[0-9]+$/",$number);
}

since preg_match returns a boolean value.

+16

- , .

else , , , , .

.

+6
+4

:

function is_numeric($number)
{
    return preg_match("/^[0-9]+$/",$number);
}

.

+4

, , , ! , : .

Steffen has a valid point. I think it depends on the size of the two code blocks. If they are more or less equal, I would use the non negated clause in the if statement.

+4
source

What about:

function is_numeric($number) {
    return preg_match("/^[0-9]+$/",$number);
}
+3
source

All Articles