A
A
Adil16032020-05-06 17:52:03
PHP
Adil1603, 2020-05-06 17:52:03

How to do if the function returns false, then output a string?

Hello. Tell me how to do it, if the function returns false, then display the line: “Incorrect array”, if there is a number, then display: “The sum of the array elements is ...”.

$test = [5,'5',5];
function summ_array ($array){
    $rezult = 0;
    if (is_array($array)){
        foreach ($array as $value){
            if (is_numeric($value)){
               $rezult = $rezult + $value;
            }

           elseif(func_num_args($value) == 0){
            	echo 'Neobhodimy parametry';}

            else {
                $rezult =  false;
            }
        }
    } else {
        $rezult = false;
    }
    return $rezult;
}

echo summ_array($test);

$summaN = [];

  echo "Сумма элементов массива равна" .summ_array($summaN);

Answer the question

In order to leave comments, you need to log in

2 answer(s)
L
Lander, 2020-05-06
@usdglander

if (summ_array($test) === false) {
    //Некорректный массив
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question