PHP Basic Tutorial Seven: Implementation of Calculator
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['value']) ? $_GET['value'] : ''; ?> <!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 = 'cm'> <form action="NumCount.php" method="post"> 数字1:<input type="text" name="num1"><br> 数字2:<input type="text" name="num2"><br> <select name = 'oper'> <option value = 'plus'>+</option> <option value = 'subtract'>-</option> <option value = 'multiply'>*</option> <option value = 'pided'>/</option> </select><br> <input type="submit" value="计算"> </form> <p><?php echo $value;?></p> </p> </body> </html>
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 './function.php'; //从html页面得到数据 $num1 = isset($_POST['num1']) ? $_POST['num1'] : 0; $num2 = isset($_POST['num2']) ? $_POST['num2'] : 0; $oper = isset($_POST['oper']) ? $_POST['oper'] : ''; //判断是否是数字 if(!is_numeric($num1) || !is_numeric($num2)){ echo "<script>alert('请输入数字')</script>"; Header("Refresh:0;url = ./CalculatingMachine.php"); } //得到计算后的值 $value = 0; //通过switch判断是那种运算 switch($oper){ case 'plus': $value = plus($num1,$num2); break; case 'subtract': $value = subtract($num1,$num2); break; case 'multiply': $value = multiply($num1,$num2); break; case 'pided': $value = pided($num1,$num2); break; default: echo ''; } //把计算后的值传递给前台。 Header("Refresh:0;url = ./CalculatingMachine.php?value={$value}");
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; }
The page of the front desk:
The data is processed in the background and then transferred to the front desk:
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 = '超全局变量'; $arr = array(1,2,3,4); //数组 var_dump($GLOBALS['a']); echo '<br>'; var_dump($GLOBALS['str']); echo '<br>'; var_dump($GLOBALS['arr']);
Copy after login
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['SERVER_ADDR']; ......结果....... 127.0.0.1
Copy after loginThe 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?>
在这里的开发都是php代码和html代码进行嵌套,数据和页面进行一起的开发,什么模式都没用到。
总结
计算机的案列几乎包含了前面的所学,把所有的都化为己用。学习过得知识要学会运用。
以上就是PHP基础教程七之计算器的实现的内容,更多相关内容请关注PHP中文网(www.php.cn)!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.
