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

菜鸡求教,该如何处理

WBOY
Release: 2016-06-13 12:13:10
Original
1002 people have browsed it

菜鸡求教
代码如下

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


输出是
为什么函数没有返回值啊?
------解决思路----------------------
第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 />
Copy after login


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


5
int(5)

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