tags:

views:

95

answers:

6

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;
}

+6  A: 

They are equivalent - choose whichever you find to be more readable.

I personally tend to leave of any else statements on conditionals if I don't need them but this is a matter of programming style rather than a matter of either programmatic performance or correctness.

Both of these code examples are functionally identical and will perform identically.

Andrew Hare
Thanks for your reply Mr. Hare :)
Gaurav Sharma
Agree, always go for the more readable option and not the easiest to write. This is based on the theory you write the code once but read it many more times.
DanDan
+16  A: 

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.

Steffen Müller
+1 Good point - no need to branch here at all.
Andrew Hare
yeah,+1 from me toovery intelligent answer.thanks Mr. Müller
Gaurav Sharma
Actually preg\_match() return either 0 or 1. But if used in a boolean context php casts them to FALSE or TRUE. In languages like C# there is no such implicit cast and you'd have to write something like `return 1===preg_match(...)`
VolkerK
Volker, you are right! Missed that point.
Steffen Müller
+4  A: 

PHP has a built in is_numeric() function: http://php.net/manual/en/function.is-numeric.php why not use that.

Crafton
:)The link "http://in.php.net/manual/en/function.is-numeric.php" says that"Numeric strings consist of optional sign, any number of digits, optional decimal part and optional exponential part. Thus +0123.45e6 is a valid numeric value. Hexadecimal notation (0xFF) is allowed too but only without sign, decimal and exponential part."
Gaurav Sharma
Try looking into the `ctype_digit()` function. However, I'm not sure whether input parameters need to be explicitly a string or whether it will accept any object type.
Martin Bean
in ctype_digit() your parameter should be enclosed in quotes. if you do like this ctype_digit(57), then this will return false
Gaurav Sharma
+4  A: 

How about:

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

This is not going to cause a performance difference though.

Matthew Flaschen
Thanks for the reply,But Steffen Müller already answered that :)
Gaurav Sharma
+3  A: 

How about:

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

From a readability standpoint it's better to have positive clauses, because people are likely to miss the ! when they read the source, which leads to wrong understanding of the code and often as result: Bugs.

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'd use the not negated clause in the if statement.

Patrick Cornelissen
yes, very true. There is a high possibility of missing the "!" while reading the source.
Gaurav Sharma