要求实现:编写一个函数(以php 面向对象的方式开发),从网页输入一个整数并打印出对应的金字塔,很多php 教程中都有涉及吧,今天以简单示例的方法展示给大家。
代码示例:
1、显示页面pview.php
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html> <head> <title>打印金字塔_脚本学堂_www.jb200.com</title> <meta http-equiv="Content-Type" content="text/html;charset=utf-8" > </head> <body> <form action="print.php" method="post"> 请输入一个数:<input type="text" name="one" /> <input type="submit" value="提交" /> </form> </body> </html>
2、打印页面:
<?php
//引入class.php 文件
require_once 'class.php';
//接收传过来的值
$one=$_REQUEST['one'];
//创建一个对象
$p = new Jzit;
//调用成员方法
$p->printd($one);
?>
3、类文件:class.php
<?php
//编写一个函数(以面向对象的方式开发),从网页输入一个整数打印出对应的金子塔
//定义一个类 用来打印金字塔 by www.jb200.com
class Jzit{
//定义接收一个值来打印金字塔成员方法
public function printd($n){
//先定义层数
for($i=1;$i<=$n;$i++){
//打印空格
for($k=1;$k<=$n-$i;$k++){
echo " ";
}
//打印*号
for($j=1;$j<=2*$i-1;$j++){
echo "*";
}
//换行,打印下一行
echo "<br />";
}
}
}
?>