第044 金字塔案例以及一些练习题

本文介绍了一个简单的PHP计算器实现方法,包括HTML表单设计用于输入数值和运算符,通过PHP后端处理计算逻辑并显示结果。此外,还提供了一个九九乘法表的PHP实现示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

image

<!doctype html>
<html lang="en">
 <head>
  <meta charset="UTF-8">
  <meta name="Generator" content="EditPlus®">
  <meta name="Author" content="">
  <meta name="Keywords" content="">
  <meta name="Description" content="">
  <title>计算器</title>
 </head>
 <body>
    <from action="" method="post">
        <table width="300px" border="0">
            <td>第一个数</td> <td><input type="text" name="num1"/></td>
            <tr/>
            <td>第二个数</td> <td><input type="text" name="num2"/></td>
            <tr/>
            <td>运算符</td>
            <td>
            <select name="oper"/>
                <option value="+">+</option>
                <option value="-">-</option>
                <option value="*">*</option>
                <option value="/">/</option>
            </select>
            </td>
            <tr>
                <td colspan="2"><input type="submit" value="计算结果"/></td>
            </tr>
        </table>
    </from>
 </body>
</html>

快看上边的表单控件 写成from了。大写的各种尴尬
【myCall.php】

<!doctype html>
<html lang="en">
 <head>
  <meta charset="UTF-8">
  <meta name="Generator" content="EditPlus®">
  <meta name="Author" content="">
  <meta name="Keywords" content="">
  <meta name="Description" content="">
  <title>计算器</title>
 </head>
 <body>

    <td>form 表单 写错了 携程from 。。。大写的尴尬,以后记住
    </td>
    <form action="result.php" method="post">
        <table width="300px" border="0">
            <td>第一个数:</td> <td><input type="text" name="num1"/></td>
            <tr/>
            <td>第二个数:</td> <td><input type="text" name="num2"/></td>
            <tr/>
            <td>运算符</td>
            <td>
            <select name="oper"/>
                <option value="+">+</option>
                <option value="-">-</option>
                <option value="*">*</option>
                <option value="/">/</option>
            </select>
            </td>
            <tr>
                <td ><input type="submit" value="计算结果"/></td>
            </tr>
        </table>
    </form>
 </body>
</html>

【result.php】

<?php
    //接收用户从myCall.php界面过来的值
    //1、接收num1
    //$_REQUEST 该方法可以接收用户的post或者get请求数据
    $num1=$_REQUEST['num1'];
    //2、接收num2
    $num2=$_REQUEST['num2'];
    //3、接收运算符
    $oper=$_REQUEST['oper'];

    echo "接收到num1= ".$num1."接收到num2= ".$num2."接收到运算符= ".$oper;
    echo "<br/>";
    switch($oper){
        case "+":
            echo "结果:".($num1+$num2);
            break;
        case "-":
            echo "结果:".($num1-$num2);
            break;
        case "*":
            echo "结果:".($num1*$num2);
            break;
        case "/":
            echo "结果:".($num1/$num2);
            break;
    }
?>  
<a href="myCall.php">跳转回计算页面</a>

在result界面 $_REQUEST[‘xx’]; xx参数如果写错了 那么就接收不到前届满传递过来的值,系统会提示 undefined index :xx 类似提示,所以一般情况下写完最好排查下

练习题:
九九乘法表:

<?php
    for($i=1;$i<=9;$i++){
        echo "<br/>";
        for($j=1;$j<=$i;$j++){
            echo "$j"."*"."$i"."=".$i*$j;
            echo " ";
        }
    }

?>

效果:
image

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

有时有晌

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值