Home > Backend Development > PHP Tutorial > PHP Basic Tutorial Seven: Implementation of Calculator

PHP Basic Tutorial Seven: Implementation of Calculator

黄舟
Release: 2023-03-06 08:52:01
Original
1810 people have browsed it

Content explained in this section

  • Implementation of calculator

  • Super global Nested php code in variable

  • html

Preface

PHP language is a language for developing server-side and processing data. The development of PHP inevitably requires interaction with the front-end page to transfer data. So how do we get data from the foreground and pass it to the backend? They use the http protocol to transmit information. You can read the blog on the other side http://www.php.cn/.

As for today’s calculator case, it is designed to transmit data to the front and back of the data. Its general function is to fill in data on the front page, submit it to the backend, process the data in the backend, and then return to the frontend.

Implementation of calculator

An html page in the front desk CalculatingMachine.php

<?php
    $value = isset($_GET[&#39;value&#39;]) ? $_GET[&#39;value&#39;] : &#39;&#39;;
?>
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>计算器的实现</title>
</head>
<style type="text/css">
    .cm{
        text-align:center;
        margin-top:100px;
        line-height:30px;
    }

</style>
<body>
    <p class = &#39;cm&#39;>
    <form action="NumCount.php" method="post">
        数字1:<input type="text" name="num1"><br>
        数字2:<input type="text" name="num2"><br>
        <select name = &#39;oper&#39;>
            <option value = &#39;plus&#39;>+</option>
            <option value = &#39;subtract&#39;>-</option>
            <option value = &#39;multiply&#39;>*</option>
            <option value = &#39;pided&#39;>/</option>
        </select><br>

        <input type="submit" value="计算">
    </form>
    <p><?php echo $value;?></p>
    </p>
</body>
</html>
Copy after login

The front desk page is to pass the user’s input data through post method NumCount.php in the background, the file suffix in the frontend ends with php. This is because when the data processed in the background is transmitted to the frontend for display, the most important point is that files with php as the suffix can write html code. However, files with html as the suffix cannot be written with php code (can be set in the configuration file);

The background processing page: NumCount.php

<?php

    //引入运算的函数
    require_once &#39;./function.php&#39;;

    //从html页面得到数据
    $num1 = isset($_POST[&#39;num1&#39;]) ? $_POST[&#39;num1&#39;] : 0;
    $num2 = isset($_POST[&#39;num2&#39;]) ? $_POST[&#39;num2&#39;] : 0;
    $oper = isset($_POST[&#39;oper&#39;]) ? $_POST[&#39;oper&#39;] : &#39;&#39;;

    //判断是否是数字
    if(!is_numeric($num1) || !is_numeric($num2)){
        echo "<script>alert(&#39;请输入数字&#39;)</script>";
        Header("Refresh:0;url = ./CalculatingMachine.php");
    }

    //得到计算后的值
    $value = 0;
    //通过switch判断是那种运算
    switch($oper){
        case &#39;plus&#39;:
            $value = plus($num1,$num2);
            break;
        case &#39;subtract&#39;:
            $value = subtract($num1,$num2);
            break;
        case &#39;multiply&#39;:
            $value = multiply($num1,$num2);
            break;
        case &#39;pided&#39;:
            $value = pided($num1,$num2);
            break;
        default:
            echo &#39;&#39;;
    }

    //把计算后的值传递给前台。
    Header("Refresh:0;url = ./CalculatingMachine.php?value={$value}");
Copy after login

The background acceptance page, because it is Data is submitted through the post method, so the data can be obtained through the super global variable $_POST[] and verified. When it is not a number, a dialog box will pop up to prompt and jump to the front desk through the header.

The data operation function in the background is encapsulated into a file, which can be used by importing the file.

The operation function is encapsulated into a file: function.php

<?php

    //加
    function plus($num1,$num2){
        return $num1 + $num2;
    }

    //减
    function subtract($num1,$num2){
        return $num1 - $num2;
    }

    //乘
    function multiply($num1,$num2){
        return $num1 * $num2;
    }

    //除
    function pided($num1,$num2){
        return $num1 / $num2;
    }
Copy after login

The page of the front desk:
PHP Basic Tutorial Seven: Implementation of Calculator

The data is processed in the background and then transferred to the front desk:
PHP Basic Tutorial Seven: Implementation of Calculator

Super global variables

In the above background code, you can see that the data is received through the super global variable $_POST[]. So what are superglobal variables in PHP and what are their functions?

Many predefined variables in PHP are "superglobal", which means they are available throughout the entire scope of a script. They can be accessed within a function or method without executing global $variable;.

Classification of super globals in PHP:

  • $GLOBALS A global combination array that contains all variables. The name of the variable is the key of the array. This means that it is available in all scopes of the script. There is no need to use global $variable; within a function or method to access it.

    <?php
    $a = 12; //整型
    $str = &#39;超全局变量&#39;;
    $arr = array(1,2,3,4); //数组
    
    var_dump($GLOBALS[&#39;a&#39;]);
    echo &#39;<br>&#39;;
    var_dump($GLOBALS[&#39;str&#39;]);
    echo &#39;<br>&#39;;
    var_dump($GLOBALS[&#39;arr&#39;]);
    Copy after login

    PHP Basic Tutorial Seven: Implementation of Calculator

You can see that the super global variable $GLOBALS automatically saves the variables in the code above.

  • #$_SERVER is an array containing information such as header, path, and script locations. The items in this array are created by the web server. There is no guarantee that every server will offer all items; servers may ignore some, or serve items not listed here.
    This super global variable also has something to do with the http protocol. In this variable, we can get some information when we are transmitting data.

    <?php
        echo $_SERVER[&#39;SERVER_ADDR&#39;];
        ......结果.......
        127.0.0.1
    Copy after login

    The above is just one of the values ​​in the server. You can get the IP address of the server. For other values, you can check the help document.

  • $_GET[] An array of variables passed to the current script via URL parameters. There are two commonly used methods for transmitting data on web pages, a GET and a POST, and this super global variable is to save the value passed through the GET method

  • $_POST[] Pass The array of variables passed to the current script by the HTTP POST method. When data is passed through POST, this super global variable will accept

  • $_REQUEST[]. In the above two super Global variables store different values ​​according to different transfer methods, and this variable will save the values ​​​​of both transfer methods.

The following super global variables will be introduced slowly in the future

  • $_FILES[] file upload variable, uploaded to via HTTP POST An array of items for the current script. I will introduce it in detail later when the file is uploaded.

  • $_COOKIE[] An array of variables passed to the current script through HTTP Cookies

  • $_SESSION[] An array of SESSION variables available to the current script array. More information on how to use it can be learned through the Session function.

The above are super global variables in PHP. We will deal with them at any time during development.

html中嵌入php代码

在上面计算器的前台代码中我们可以看到,当数据处理完传递到前台后,通过在p标签中写php代码来显示数据。从中我们可以看到php是怎么嵌套在html代码中

    <?php code?>
Copy after login

在这里的开发都是php代码和html代码进行嵌套,数据和页面进行一起的开发,什么模式都没用到。

总结

计算机的案列几乎包含了前面的所学,把所有的都化为己用。学习过得知识要学会运用。

 以上就是PHP基础教程七之计算器的实现的内容,更多相关内容请关注PHP中文网(www.php.cn)!


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template