• 2018.5.14 PHP基础学习


    1.使用PHP输出HTML

    使用PHP输出一个表格,并且通过style标签改变字体

    <!--思考与练习-->
    
    <style type="text/css">
        table{
            font-family:"黑体";
            font-size: 23px;
            color: red;
        }
    
        a{
            font-family: "幼圆";
            font-size: 100px;
        }
    </style>
    
    <?php
    /**
     * Created by PhpStorm.
     * User: qichunlin
     * Date: 2018/5/10
     * Time: 下午5:39
     */
    $XH="081101";
        echo "<script>";
        echo "('我是Legend')";
        echo "</script>";
        echo "<table width=200 border=1 align=center>";
        echo "<tr><td>学号</td><td>姓名</td><td>性别</td></tr>";
        echo "<tr><td>$XH</td><td>王林</td><td>男</td></tr>";
        echo "<tr><td>081102</td><td>程明</td><td>男</td></tr>";
        echo "</table>";
        echo "<a href='www.baidu.com'>sss</a>";
    ?>
    

    2.在HTML中嵌入PHP代码

    <html>
    <head>
        <title>HTML中嵌入PHP</title>
    </head>
    <body>
        <?php
        /**
         * 在文本框中输出PHP内容
         * Created by PhpStorm.
         * User: qichunlin
         * Date: 2018/5/10
         * Time: 下午5:44
         */
        $XH="081101";
        ?>
        学号:<input type="text" name="tx" size="20" value="<?php echo $XH?>"><br/>
    </body>
    </html>
    
    

    3.PHP变量的使用

    由用户输入一元二次方程的系数,根据系数求一元二次方程的解,测试运行结果

    <html>
    <body>
    <div>
        <font size="5" color="aqua" face="宋体"><p align="center">计算一元二次方程式</p></font>
    </div>
    <form action="" method="post">
        <table border="1" align="center" cellpadding="0">
            <tr>
                <td colspan="2" align="center" bgcolor="#999999">输入表单</td>
            </tr>
            <tr>
                <td>系数a:</td>
                <td><input type="text" name="a"></td>
            </tr>
            <tr>
                <td>系数b:</td>
                <td><input type="text" name="b"></td>
            </tr>
            <tr>
                <td>系数c:</td>
                <td><input type="text" name="c"></td>
            </tr>
            <tr>
                <td colspan="2" align="center">
                    <input type="submit" name="submit" value="Submit">
                    <input type="reset" name="reset" value="Reset"></td>
            </tr>
        </table>
        <br/><br/><br/>
    </form>
    </body>
    </html>
    
    <?php
    /**
     * Created by PhpStorm.
     * User: qichunlin
     * Date: 2018/5/12
     * Time: 下午5:27
     */
    if (isset($_POST['submit'])) {
        $a = $_POST['a'];
        $b = $_POST['b'];
        $c = $_POST['c'];
        $result = $b * $b - 4 * $a * $c;
        if ($result >= 0) {
            $x1 = (-$b + sqrt($result)) / (2 * $a);
            $x2 = (-$b - sqrt($result)) / (2 * $a);
            echo "<script>alert('Enter a Number:a=" . $a . " ,b=" . $b . " ,c=" . $c . ";The Result is:x=" . $x1 . ",x2=" . $x2 . "');</script>";
        } else {
            echo "<script>alert('方程无解');</script>";
        }
    }
    ?>
    <table align="center" border="1">
        <th colspan="2">Data Result</th>
        <tr>
            <td>One Root:</td>
            <td><input type="text" disabled="disabled" value=<?php echo $x1; ?>></td>
        </tr>
        <tr>
            <td>Two Root:</td>
            <td><input type="text" disabled="disabled" value=<?php echo $x2; ?>></td>
        </tr>
    </table>
    

    3.2获取登陆表单中的用户名和密码,如果用户名是“admin” 密码是“12345” 就显示登录成功

    <!--登陆表单-->
    <html>
    <body>
    <form action="" method="post">
        <table border="1" align="center" cellspacing="0" cellpadding="0">
            <tr>
                <td colspan="2" align="center" bgcolor="#999999">用户登陆表单</td>
            </tr>
            <tr>
                <td>用户名:</td>
                <td><input type="text" name="username"></td>
            </tr>
            <tr>
                <td>密码:</td>
                <td><input type="password" name="password"></td>
            </tr>
            <tr>
                <td colspan="2" align="center">
                    <input type="submit" name="submit" value="Submit">
                    <input type="reset" name="submit2" value="Reset"></td>
            </tr>
        </table>
        <br/><br/><br/>
    </form>
    </body>
    </html>
    
    <?php
    /**
     * Created by PhpStorm.
     * User: qichunlin
     * Date: 2018/5/8
     * Time: 下午10:31
     */
    if (isset($_POST['submit'])) {
        $username = $_POST['username'];
        $password = $_POST['password'];
        if ($username == "admin" && $password == "123456") {
            echo "<script>alert('Successful Login');</script>";
        } else {
            echo "<script>alert('Failure Login');</script>";
        }
    }
    ?>
    
    
    

    4.函数的定义与使用

    定义一个函数,用于比较两个数的大小

    <html>
    <title>实验3_5思考与练习</title>
    <body>
    <form action="" method="post">
        <div><font>比较两个的大小数</font></div>
        Number1:<input type="text" name="num1"><br><br>
        Number2:<input type="text" name="num2"><br><br>
        <input type="submit" name="submit" value="Submit">
    
    </form>
    
    </body>
    <?php
    /**
     * Created by PhpStorm.
     * User: qichunlin
     * Date: 2018/5/12
     * Time: 下午2:03
     */
    
    if (isset($_POST['submit'])) {
        $num1 = $_POST['num1'];
        $num2 = $_POST['num2'];
        $a = Comp($num1, $num2);
    
    }
    function Comp($num1, $num2)
    {
        if ($num1 > $num2) {
            $bool = $num1;
        } else {
            $bool = $num2;
        }
        echo "比较结果:<input type='text' value='{$bool}'>";
    
    }
    
    ?>
    </html>
    
    

    4.2排序函数,接收用户输入的值使用该函数对其进行排序

    <!--实验3.5-->
    <?php
    /**
     * Created by PhpStorm.
     * User: qichunlin
     * Date: 2018/5/11
     * Time: 下午8:16
     */
    //定义排序函数
    function my_sort($array)
    {
        for ($i = 0; $i < count($array); $i++) {
            for ($j = $i + 1; $j < count($array); $j++) {
                //判断大小  交换值
                if ($array[$i] > $array[$j]) {
                    $temp = $array[$j];
                    $array[$j] = $array[$i];
                    $array[$i] = $temp;
                }
            }
        }
        return $array;//返回排序后的数组
    }
    
    echo "请输入需要排序的数据:<br/>";
    echo "<form method='post'> ";
    for ($i = 1; $i < 6; $i++) {
        //文本框的名字是数组名
        echo "<input type='text' name='stu[]' size='5'>";
        if ($i < 5)
            echo "-";
    }
    echo "<input type='submit' name='bt' value='提交'  >";
    echo "</form>";
    if (isset($_POST['bt'])) {
        $stu = $_POST['stu'];
        $arr_stu = my_sort($stu);
        echo "排序后的数据如下图所示:<br/>";
        while (list($key, $value) = each($arr_stu)) {
            echo $value . "<br/>";//输出排序后的值
        }
    }
    ?>
    
    
    

    5.流程控制

    计算从1开始到指定整数的累加和,指定数字有用户输入

    <html>
    <body>
    <div><font size=4 color=blue face="黑体"><p align=center>计算累加和</font></div>
    <form action="" method="post">
        <p align="center">
        1+2+....+<input type="text" name="num"  size="5">
        <input type="submit" name="submit" value="计算">
        </p>
    </form>
    </body>
    </html>
    
    <?php
    /**
     * Created by PhpStorm.
     * User: qichunlin
     * Date: 2018/5/10
     * Time: 下午6:39
     */
    if(isset($_POST['submit'])) {
        $num=$_POST['num'];
        $i=0;
        $sum = 0;
        if($num<=2)
            echo "<script>alert('请输入大于2的数字')</script>";
        else{
            while ($i<$num){
                $sum=$sum+$i;
                $i++;
            }
            echo "<script>alert('1+2+...+$num=$sum');</script>";
        }
    
    }
    ?>
    

    5.2计算一个指定整数的阶乘,要求先判断用户的输入是否不等于0的整数

    <html>
    <title>实验3_4思考与练习</title>
    <style type="text/css">
        .btn {
            font-family: "黑体";
             150px;
            height: 50px;
            font-size: 30px;
        }
    </style>
    <body>
    <div><font size=4 color=blue face="黑体"><p align=center>计算求阶乘</font></div>
    <form action="" method="post">
        <p align="center">
            Input Number:<input type="text" name="num" value=""><br><br>
            <input type="submit" class="btn" name="submit" value="Submit">
        </p>
    </form>
    </body>
    </html>
    
    <?php
    if (isset($_POST['submit'])) {
        $num = $_POST['num'];
        //$i=0;
        if ($num > 0) {
            $result = 1;
            for ($i = 1; $i <= $num; $i++) {
                $result = $result * $i;
            }
        }
    
    }
    ?>
    <p align="center">
        The Result is:<input type="text" disabled="disabled" size="20px" value="<?php echo $result; ?>">
    </p>
    
    
    

    6.php输出乘法表

    <?php
    /**
     * Created by PhpStorm.
     * User: qichunlin
     * Date: 2018/5/07
     * Time: 下午7:39
     */
        for ($i=1;$i<=9;$i++){
            for ($j=1;$j<=$i;$j++){
                echo "$i * $j=".$i*$j."&nbsp";
                if ($i == $j)
                echo "<br>";//九九乘法表
            }
        }
    ?>
    

    7.PHP实例-----多项选择题

    优化解决了进入网页显示请选择答案后的错误答案

    <!DOCTYPE html>
    <title>H5 表单,包含五个复选框和一个提交按钮</title>
    <form action="" method="post">
        以下属于Web开发语言的有哪几种?<br/>
        <input type="checkbox" id = 'answer' name="answer" value="C语言">C语言<br>
        <input type="checkbox" id = 'answer' name="answer" value="PHP">PHP<br>
        <input type="checkbox" id = 'answer' name="answer" value="FLASH">FLASH<br>
        <input type="checkbox" id = 'answer' name="answer" value="ASP">ASP<br>
        <input type="checkbox" id = 'answer' name="answer" value="JSP">JSP<br>
        <input type="submit" name="bt_answer" id="btn" style=" 100px" value="提交">
    </form>
    <script src="https://code.jquery.com/jquery-3.3.1.min.js"
            integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8="
            crossorigin="anonymous"></script>
    <script>
        $('#btn').on('click',function () {
            var check_val = [];
            var temp = '';
            $('input[name="answer"]:checked').each(function () {
                check_val.push($(this).val());
                temp+=$(this).val();
            });
            if(check_val.length == 0){
                alert('请选择');
            }else{
                if(temp == 'PHPASPJSP'){
                    alert('回答正确');
                }else{
                    alert('回答错误');
                }
            }
        })
    </script>
    
    <?php
    /**
     * Created by PhpStorm.
     * User: qichunlin
     * Date: 2018/5/10
     * Time: 下午2:13
     */
    
    //if (isset($_POST['bt_answer'])) {
    //    $answer = @$_POST['answer'];
    //    if (empty($answer))
    //        echo "<script> alert('答案不能为空!')</script>";
    //    $nun = count($answer);
    //    $anw = "";
    //    for ($i = 0; $i < $num; $i++) {
    //        $anw = $anw . $anwser[$i];
    //    }
    //
    //    if ($anw == "PHPASPJSP") {
    //        echo "<script> alert('回答正确!')</script>";
    //    } else {
    //        echo "<script> alert('回答错误!')</script>";
    //    }
    //}
    
    ?>
    
  • 相关阅读:
    Git安装配置
    Openstack 错误日志查看方法
    keystone v3.0与2.0的区别
    Python远程调试Openstack
    openstack遇到的错误
    特别翔实的adaboost分类算法讲解 转的
    h5 html5 模拟时钟 页面
    js 面向对象 jquery 全局变量 封装
    HTML5 h5 微信 浮层 提示 点击右上角,从浏览器打开 pop.png
    jquery中ajax使用error调试错误的方法,实例分析了Ajax的使用方法与error函数调试错误的技巧
  • 原文地址:https://www.cnblogs.com/qichunlin/p/9029513.html
Copyright © 2020-2023  润新知