让php小白看的php类小程序

写一个针对php小白看的关于类与对象的小程序:

该程序由三个程序组合而成,主要是输出金字塔:
input.php:
<html>
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
<form action="print.php" method="post">
请输入金字塔的层数:<input type="text" name="layer">
    <br>
    <input type="submit" value="打印">
</form>
</body>
</html>

print.php

<?php
//接收MyInputView页面传来的数据
        require_once "Print.class.php";
$layer = $_REQUEST["layer"];
        //调用一个类来完成打印
        $a = new abc();

        $a->printStar($layer);

?>

Print.class.php
<?php
    class abc{
        //接收一个数,并打印金字塔
        public function printStar($n){
            //这里需要使用for循环了
            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>";
            }
        }
}

?>





希望能够帮助更好的理解一下PHP中的类与对象概念。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值