


PHP study notes - simple calculator script PHP from entry to proficiency PHP learning website php100
What is implemented in PHP this time is: the user inputs two numbers, and then selects one or more of the four operators of addition, subtraction, multiplication and division to display the calculation results.
My idea is this: create a form in HTML, including: 1. Selection box, allowing the user to select addition, subtraction, multiplication and division operations, and the selected results are stored in the operation array; 2. Text box, allowing the user to enter the required The calculated numbers and input results are stored in the num array; 3. Submit button to submit the form content. The form is submitted directly to this php page for processing using the POST method, where $_POST['operation'] stores operator information, and $_POST[num'] stores the data to participate in the operation. Determine the operator in the php script, perform the corresponding calculation, save the result to the array of $msg, and finally output $msg.
Okay, let’s go straight to the code:
<?php $result=array(); //用来保存计算结果的数组 $msg=array(); //保存结果消息的数组 $i=0; //结果的个数 $error=""; //错误消息 if(isset($_POST['operation'])){ //如果已经选择了运算符 if((""!=$_POST['num'][0])&&""!=($_POST['num'][1])){ //输入文档框内容部位空 $num1=(double)$_POST['num'][0]; //从字符串强制转换成double型的类型数据 $num2=(double)$_POST['num'][1]; foreach($_POST['operation'] as $op){ //读取所选择的运算符 switch($op){ //判断运算符属于哪一类 case 'add': $result[$i]=$num1+$num2; //加法 $msg[$i]="$num1"."+"."$num2"."="."$result[$i]";//将完整的算数式保存到消息数组里面 $i++; break; case 'sub': $result[$i]=$num1-$num2; //减法 $msg[$i]="$num1"."-"."$num2"."="."$result[$i]";//将完整的算数式保存到消息数组里面 $i++; break; case 'mul': $result[$i]=$num1*$num2; //乘法 $msg[$i]="$num1"."*"."$num2"."="."$result[$i]";//将完整的算数式保存到消息数组里面 $i++; break; case 'div': if($_POST['num'][1]!=0){ //保证被除数不能为0 $result[$i]=$num1/$num2; //除法 $msg[$i]="$num1"."/"."$num2"."="."$result[$i]"; //将完整的算数式保存到消息数组里面 $i++; } else $error="被除数不能为0\n" ; //如果除数为0,错误消息有提示 break; } } } else { //输入的数字有为空的情况 if( ""!=$_POST['num'][0] ) $error.="请输入num 1 \n"; //记录到错误消息中 if( ""!=$_POST['num'][1] ) $error.="请输入num 2 \n"; } } ?>
"; foreach($msg as $str) echo $str."
"; echo $error; } ?>
The running interface is as follows:
Input 13, 12
If all operators are selected:
Ratings and improvements:
After testing, it was found that there are some defects. For example: every time after inputting data and submitting, the calculation results are displayed, but the page is also updated, and the originally entered data is gone. The improved result should be like this: after each submission, the text box will save the last record, but the check box will not be saved. The specific implementation is to set properties in the text box. I'm not particularly familiar with this, and I'm too lazy to do it now, so I'll just put it aside for now.
As for the code, there are many variables used in the PHP script, and the memory consumption is correspondingly large, so if variables such as $result[], $num1, and $num2 are not called by other scripts, they can be omitted; but in order Better scalability. When adding functions, you do not need to significantly change the original code. It is better to keep it.
Interested readers can go to http://www.beartracker.top/server1.php to test. Corrections are welcome ^_^
The above introduces the PHP learning notes - a simple calculator script, including PHP learning content. I hope it will be helpful to friends who are interested in PHP tutorials.

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



With the development of the Internet, the demand for dynamic web pages is increasing. As a mainstream programming language, PHP is widely used in web development. So, for beginners, how to learn PHP development? 1. Understand the basic knowledge of PHP. PHP is a scripting language that can be directly embedded in HTML code and parsed and run through a web server. Therefore, before learning PHP, you can first understand the basics of front-end technologies such as HTML, CSS, and JavaScript to better understand the operation of PHP.

PHP study notes: Web crawler and data collection Introduction: A web crawler is a tool that automatically crawls data from the Internet. It can simulate human behavior, browse web pages and collect the required data. As a popular server-side scripting language, PHP also plays an important role in the field of web crawlers and data collection. This article will explain how to write a web crawler using PHP and provide practical code examples. 1. Basic principles of web crawlers The basic principles of web crawlers are to send HTTP requests, receive and parse the H response of the server.

PHP study notes: Modular development and code reuse Introduction: In software development, modular development and code reuse are very important concepts. Modular development can decompose complex systems into manageable small modules, improving development efficiency and code maintainability; while code reuse can reduce redundant code and improve code reusability. In PHP development, we can achieve modular development and code reuse through some technical means. This article will introduce some commonly used technologies and specific code examples to help readers better understand and apply these concepts.

PHP study notes: Performance analysis and tuning Introduction: In web development, performance is a very critical factor. A high-performance website can provide a better user experience, improve user retention, and increase business revenue. In PHP development, performance optimization is a common and important issue. This article will introduce performance analysis and tuning methods in PHP, and provide specific code examples to help readers better understand and apply these techniques. 1. Performance analysis tool Xdebug extension Xdebug is a powerful P

What is the best way to learn PHP in 2023? With the rapid development of the Internet, computer programming has become a skill with extremely high employment prospects. Among many programming languages, PHP is a language that is widely used in network development. If you want to learn PHP, it's important to know the best way to learn it. PHP is an open source, server-side scripting language used to develop dynamic websites and applications. Compared to other languages, PHP has a lower learning curve and a wide range of applications, making it an ideal choice for beginners.

Learn video special effects and filter processing function methods in PHP. PHP is a powerful programming language that is widely used in the field of web development. With the development of website design, video special effects and filter processing are becoming more and more popular. This article will introduce how to use PHP to implement video special effects and filter processing, as well as some commonly used function methods. 1. Install the ffmpeg extension. To process videos, we need to install the ffmpeg extension. Through this extension, we can directly call the ffmpeg command in PHP for video processing. Installation process

PHP study notes: Overview of front-end and back-end separation and API design: With the continuous development of the Internet and the increasing user needs, the development model of front-end and back-end separation has attracted more and more attention from developers. Front-end and back-end separation refers to separating the development of the front-end and the back-end, and conducting data interaction through APIs to achieve development efficiency and flexibility. This article will introduce the concept of front-end and back-end separation, and how to design an API. The concept of front-end and back-end separation: The traditional Web development model is front-end and back-end coupling, that is, the development of front-end and back-end is carried out in the same project.

PHP study notes: Form processing and data validation In web development, forms are one of the important components for users to interact with the website. When users fill out forms and submit data on the website, the website needs to process and verify the submitted data to ensure the accuracy and security of the data. This article will introduce how to use PHP to process forms and perform data validation, and provide specific code examples. Form submission and data preprocessing In HTML, we need to use the <form> tag to create a form and specify the form
