<?php
//递归实现字符串翻转
function reverse_r($str){
if(strlen($str)>0){
reverse_r(substr($str,1));
}
echo substr($str,0,1);
return;
}
//循环实现字符串翻转
function reverse_i($str){
for($i=1; $i<=strlen($str);$i++){
echo substr($str,-$i,1);
}
return;
}
reverse_r("Hello world");
$num = 6;
$start = 0;
function tree($start,$max){ //递归星星树
$start++;
if($start < $max){
echo "<br />";
for($a = ($max-$start)/2;$a > 0;$a--){
echo " ";
}
for($a = $start;$a > 0;$a--){
echo "*";
}
tree($start,$max);
}
echo "<br />";
for($a = ($max-$start)/2;$a > 0;$a--){
echo " ";
}
for($a = $start;$a > 0;$a--){
echo "*";
}
return; //递归关键要使用return ,如果使用了exit就不能正常显示了
}
tree($start,$num);
echo "<br />";
function reverse_num($num){ //递归反转数字
$num--;
if($num > 0){
echo $num;
reverse_num($num);
}
echo $num;
return;
}
reverse_num($num);
//递归实现字符串翻转
function reverse_r($str){
if(strlen($str)>0){
reverse_r(substr($str,1));
}
echo substr($str,0,1);
return;
}
//循环实现字符串翻转
function reverse_i($str){
for($i=1; $i<=strlen($str);$i++){
echo substr($str,-$i,1);
}
return;
}
reverse_r("Hello world");
$num = 6;
$start = 0;
function tree($start,$max){ //递归星星树
$start++;
if($start < $max){
echo "<br />";
for($a = ($max-$start)/2;$a > 0;$a--){
echo " ";
}
for($a = $start;$a > 0;$a--){
echo "*";
}
tree($start,$max);
}
echo "<br />";
for($a = ($max-$start)/2;$a > 0;$a--){
echo " ";
}
for($a = $start;$a > 0;$a--){
echo "*";
}
return; //递归关键要使用return ,如果使用了exit就不能正常显示了
}
tree($start,$num);
echo "<br />";
function reverse_num($num){ //递归反转数字
$num--;
if($num > 0){
echo $num;
reverse_num($num);
}
echo $num;
return;
}
reverse_num($num);