Example #1
0
function sumNumbers($inputFile)
{
    $sum = 0;
    foreach ($inputFile as $value) {
        if (is_object($value) || is_array($value)) {
            $sum += sumNumbers($value);
        }
        if (is_numeric($value)) {
            $sum += $value;
        }
    }
    return $sum;
}
Example #2
0
function sumNumbers($inputFile)
{
    $sum = 0;
    foreach ($inputFile as $value) {
        if (is_object($value)) {
            $ignoreVal = in_array("red", (array) $value, true);
            if ($ignoreVal === false) {
                $sum += sumNumbers($value);
            }
        }
        if (is_array($value)) {
            $sum += sumNumbers($value);
        }
        if (is_numeric($value)) {
            $sum += $value;
        }
    }
    return $sum;
}
Example #3
0
<?php

//Write a PHP script SumTwoNumbers.php that declares two variables, firstNumber and secondNumber. They should hold integer or floating-point numbers (hard-coded values). Print their sum in the output in the format shown in the examples below. The numbers should be rounded to the second number after the decimal point. Find in Internet how to round a given number in PHP.
function sumNumbers($first, $second)
{
    echo number_format($first + $second, 2, '.', '') . "\n";
}
sumNumbers(2, 5);
sumNumbers(1.567808, 0.356);
sumNumbers(1234.5678, 333);