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"; } } ?>
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.