php遞迴的新有關問題

來源:互聯網
上載者:User
php遞迴的新問題
本帖最後由 iamsai 於 2013-09-28 14:04:34 編輯


reverse_i('hello');

function reverse_i($str)
{

if(strlen($str)>0)

reverse_i(substr($str,1));

echo substr($str,0,1);

//return

}
/*
大家看我的思路對嗎?
reverse('hello');

function reverse_i('hello')
if(strlen('hello'))>0
reverse_i('ello');

function reverse_i('ello')
if(strlen('ello'))>0
reverse_i('llo');

function reverse_i('llo')
if(strlen('llo'))>0
reverse_i('lo');

function reverse_i('lo')
if(strlen('lo'))>0
reverse_i('o');

function reverse_i('o')
if(strlen('o'))>0
reverse('');

function reverse_i('')
if(strlen(''))>0
此時不滿足if條件就跳出if語句
執行下一句
echo substr($str,0,1);
列印出一個Null 字元程式不就結束了嗎?因為沒有return語句也就沒有返回調用它的return('o')也無法順勢列印出後面的olleh呀
可程式輸出仍然是olleh...
*/

?>

分享到:


------解決方案--------------------
很明顯是要輸出的呀。。
你的程式可以看成這樣
$str=123;
if(strlen('123')>0)

//reverse_i('23');
if(strlen('23')>0){
//reverse_i('3');
if(strlen('3')>0){
//reverse_i(''); 不合,然後執行後面語句
echo substr('3',0,1);//輸出3
}
echo substr('23',0,1);//輸出2
}

echo substr('123',0,1);//輸出1
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.