php输出金字塔的2种实现方法,php输出金字塔2种
本文实例讲述了php输出金字塔的2种实现方法。分享给大家供大家参考。具体分析如下:
下面给大家总结了两种实现金字塔打印的方法,一种是利用了自定义函数,另一种是利用了for循环了,其实两都用到了只是前者高级一些吧.
自定义函数实现金字塔,代码如下:
复制代码 代码如下:
/**
* 金字塔
* string fun_py(int $rows = 9, bool $sort=true)
* $rows 表示行数 必须为整数 并且必须在1-20之间
* $sort 表示排序 true表示正序 FALSE表示倒序
*/
function fun_py($rows = 9, $sort=true){
if ($rows20){
return "必须在1-20之间";
}
if($rows!=(int)($rows)){
return '行数 必须为整数';
}
$str="";
if($sort){
for($i=1;$i
$str.= '
';
for($j=1;$j
if($j==1){
for($k=1;$k
$str.= ' ';
}
}
$str.= '*'.' ';
}
}
} else{
for($i=$rows;$i>=1;$i--){
$str.= '
';
for($j=1;$j
if($j==1){
for($k=1;$k
$str.= ' ';
}
}
$str.= '*'.' ';
}
}
}
return $str;
}
echo fun_py(9,false);
?>
下面再实现个金字塔形状物,通用也是用到for循环,代码如下:
复制代码 代码如下:
/**
金字塔正序
**/
for($a=1;$a
for ($b=10;$b>=$a;$b--){
echo " ";
}
for ($c=1;$c
echo "*"." ";
}
echo "
";
}
?>
同样想让这个金字塔倒立起来呢,代码如下:
复制代码 代码如下:
/**
金字塔玩倒立
**/
for($a=10;$a>=1;$a--){
for ($b=10;$b>=$a;$b--){
echo " ";
}
for ($c=1;$c
echo "*"." ";
}
echo "
";
}
?>
希望本文所述对大家的PHP程序设计有所帮助。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

Atom editor mac version download
The most popular open source editor

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SublimeText3 Linux new version
SublimeText3 Linux latest version

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment
