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

初学函数遇到一个小疑点

WBOY
Release: 2016-06-13 12:25:28
Original
911 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);
?>
啥也没输出,傻眼了
        
------解决思路----------------------

<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 />
Copy after login

Related labels:
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