Home >Backend Development >PHP Tutorial >初学函数遇到一个小问题

初学函数遇到一个小问题

WBOY
WBOYOriginal
2016-06-23 13:25:20868browse

定义一个可以获取指定整数数组元素和的函数并调用。
我的代码是:
      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);?>

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