Show DevBest [PHP] Simple 'sum' function

Status
Not open for further replies.

Markshall

Русский Стандарт
Contributor
Dec 18, 2010
2,638
2,393
It's nothing special, nothing special at all.

I get bored of constantly typing:
PHP:
$i = 4 + 6 + 7 + 1;
or whatever, so I made a function sort of like an array that you put numbers into and it adds them together by itself and returns the outputted sum.

Here:
PHP:
<?php
function sum()
{
    $i = 0;
    foreach ( func_get_args() as $k )
    {
        $i += ( is_numeric( $k ) ) ? $k : 0;
    }
    return $i;
}

echo sum( 4, 5, 67, 43, 8, 23 ); //result: 150
?>

Usage is very easy and is demonstrated in the code above ^

Hope it helps if you use it,
Mark.
 
Status
Not open for further replies.

Users who are viewing this thread

Top