Home >Backend Development >PHP Tutorial >初学函数遇到一个小疑点

初学函数遇到一个小疑点

WBOY
WBOYOriginal
2016-06-13 12:25:28900browse

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

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

Statement:
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