Home >Backend Development >PHP Tutorial >菜鸡求教,该如何处理

菜鸡求教,该如何处理

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-06-13 12:13:101015browse

菜鸡求教
代码如下

<?php<br />function Contrast($arr,$r,$c,&$numb){<br />	if($arr[$r][$c]==$arr[$r][$c+1]){<br />		$c++;<br />		$numb++;		<br />		Contrast($arr,$r,$c,$numb);		<br />	}else{	  <br />	  echo $numb."</br>";<br />	  return  $numb;	  <br />	}<br />}<br /><br />$arr=array(array(0,0,0,1,1,1,1,1,1,0,0,));<br />$r=0;<br />$c=3;<br />$numb=0;<br />$d=Contrast($arr,$r,$c,$numb);<br />var_dump($d);<br />?>


输出是
为什么函数没有返回值啊?
------解决思路----------------------
第6行 加上return  :return Contrast($arr,$r,$c,$numb);
------解决思路----------------------
這個分支的遞歸沒有返回值
<br />    if($arr[$r][$c]==$arr[$r][$c+1]){<br />        $c++;<br />        $numb++;       <br />        Contrast($arr,$r,$c,$numb);       <br />    }else{ <br />


改為:
<br /><?php<br />function Contrast($arr,$r,$c,&$numb){<br />    if($arr[$r][$c]==$arr[$r][$c+1]){<br />        $c++;<br />        $numb++;       <br />        return Contrast($arr,$r,$c,$numb);       <br />    }else{      <br />      echo $numb."</br>";<br />      return  $numb;      <br />    }<br />}<br /> <br />$arr=array(array(0,0,0,1,1,1,1,1,1,0,0,));<br />$r=0;<br />$c=3;<br />$numb=0;<br />$d=Contrast($arr,$r,$c,$numb);<br />var_dump($d);<br />?><br />


5
int(5)

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