Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What approach of functions shall I use

Tags:

function

php

A very basic question it is but I wanted expert advice that is why posting it here.

Here are two functions,
what is the difference between the two ? Are both of them equivalently efficient and includes best practices or Which one of them is better to use 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;
}
like image 535
Gaurav Sharma Avatar asked Feb 03 '10 11:02

Gaurav Sharma


1 Answers

Some coding standards state that the first branch should be the one that is more likely, while the else branch should cope with the more exceptional things.

But this is totally esoteric, choose whatever you want.

In my personal opinion, rather use

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

as preg_match returns a boolean.

like image 60
Steffen Müller Avatar answered Oct 04 '22 22:10

Steffen Müller