鍍金池/ 問答/PHP/ 函數(shù)的結(jié)果怎么賦值給變量啊

函數(shù)的結(jié)果怎么賦值給變量啊

<?php
  $name = 'stefen';
  $tel = '10795856';
  $motto = 'If you shed tears when you miss the sun,then you would miss the stars';

  function totalLen(...$string){
    //=print_r($string);
    foreach ($string as $content) {
      echo strlen($content).',';
    }
  }

  $arrNum = totalLen($name,$tel,$motto);
  //totalLen($name,$tel,$motto)會得到6,8,69,但是貌似不能賦值給$arrNum

  echo array_sum(array($arrNum));

我想實現(xiàn)的功能是,無論有多少變量傳入函數(shù),都能自動計算出所有字符的長度的和,但是現(xiàn)在沒辦法把得到的結(jié)果賦值給變量,求指教

回答
編輯回答
紓惘
function totalLen(...$string){
    $lens = [];
    foreach ($string as $content) {
      $lens[] = strlen($content);
    }
    reutrn $lens;
}
2017年12月2日 14:50