初学函数遇到一个小问题

WBOY
Release: 2016-06-23 13:25:20
Original
861 people have browsed it

定义一个可以获取指定整数数组元素和的函数并调用。
我的代码是:
      function sum($str)
      {
             for($i=0;$i                      $sum=0;
                      $sum=$sum+$str[$i];
              }
              return $sum;
        }
        $a=array(7,9,8,5,3);
        sum($a);
?>
啥也没输出,傻眼了
        


回复讨论(解决方案)

<?php      function sum($str)      {     $sum=0;             for($i=0;$i<count($str);$i++){                      //$sum=0;                      $sum=$sum+$str[$i];              }              return $sum;//你这只是返回了值,没输出,        }        $a=array(7,9,8,5,3);        echo sum($a);?>
Copy after login

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template