PHP has made a calculator. if something goes wrong, solve it! -Php Tutorial

Source: Internet
Author: User
PHP has made a calculator. if something goes wrong, solve it! Here is the processing code for making 0 inputs:

========================================================== ======================================

If ($ _ POST ['num0']) {
$ Num = $ _ POST ['num0'];
If (is_dir ('txt ')){
$ Open = fopen ("txt/caculator.txt", "r + ");
If (is_file ("txt/caculator.txt ")){
$ A = file_get_contents ("txt/caculator.txt ");
$ E = (int) $;
$ B = $ e. "0 ";
Echo $ B;
File_put_contents ("txt/caculator.txt", $ B );
Fclose ($ open );
}
}
}
========================================================== ======================================
This is the form:
========================================================== ======================================









Calculator


If (! Is_dir ('txt ')){
If (! Is_file ("txt/caculator.txt ")){
Echo "success
Caculator2.txt caculator3.txt can complete the task normally! '); Script ";
}}
If ($ _ POST ['num0']) {
$ Num = $ _ POST ['num0'];
If (is_dir ('txt ')){
$ Open = fopen ("txt/caculator.txt", "r + ");
If (is_file ("txt/caculator.txt ")){
$ A = file_get_contents ("txt/caculator.txt ");
$ E = (int) $;
$ B = $ e. "0 ";
Echo $ B;
File_put_contents ("txt/caculator.txt", $ B );
Fclose ($ open );
}
}
}
// Click the response in number 1
If ($ _ POST ['num1']) {
$ Num = $ _ POST ['num1'];
If (is_dir ('txt ')){
$ Open = fopen ("txt/caculator.txt", "r + ");
If (is_file ("txt/caculator.txt ")){
$ A = file_get_contents ("txt/caculator.txt ");
$ B = $ a. $ num;
File_put_contents ("txt/caculator.txt", $ B );
Fclose ($ open );
}
}
}

If ($ _ POST ['num2']) {
// Click the response of number 2
$ Num2 = $ _ POST ['num2'];
If (is_dir ('txt ')){
If (is_file ("txt/caculator.txt ")){
$ Open = fopen ("txt/caculator.txt", "r + ");
$ Open;
$ A = file_get_contents ("txt/caculator.txt ");
$ B = $ a. $ num2;
File_put_contents ("txt/caculator.txt", $ B );
Fclose ($ open );
}
}
}
// Click the response in number 3
If ($ _ POST ['num3']) {

$ Num = $ _ POST ['num3'];
If (is_dir ('txt ')){
If (is_file ("txt/caculator.txt ")){
$ Open = fopen ("txt/caculator.txt", "r + ");
$ Open;
$ A = file_get_contents ("txt/caculator.txt ");
$ B = $ a. $ num;
File_put_contents ("txt/caculator.txt", $ B );
Fclose ($ open );
}
}
}
// Click the response in number 4
If ($ _ POST ['num4']) {

$ Num = $ _ POST ['num4'];
If (is_dir ('txt ')){
If (is_file ("txt/caculator.txt ")){
$ Open = fopen ("txt/caculator.txt", "r + ");
$ Open;
$ A = file_get_contents ("txt/caculator.txt ");
$ B = $ a. $ num;
File_put_contents ("txt/caculator.txt", $ B );
Fclose ($ open );
}
}
}
// Click the response in number 5
If ($ _ POST ['num5']) {

$ Num = $ _ POST ['num5'];
If (is_dir ('txt ')){
If (is_file ("txt/caculator.txt ")){
$ Open = fopen ("txt/caculator.txt", "r + ");
$ Open;
$ A = file_get_contents ("txt/caculator.txt ");
$ B = $ a. $ num;
File_put_contents ("txt/caculator.txt", $ B );
Fclose ($ open );
}
}
}
// Click the response in Number 6
If ($ _ POST ['num6']) {

$ Num = $ _ POST ['num6'];
If (is_dir ('txt ')){
If (is_file ("txt/caculator.txt ")){
$ Open = fopen ("txt/caculator.txt", "r + ");
$ Open;
$ A = file_get_contents ("txt/caculator.txt ");
$ B = $ a. $ num;
File_put_contents ("txt/caculator.txt", $ B );
Fclose ($ open );
}
}
}
// Click the response in number 7
If ($ _ POST ['num7']) {

$ Num = $ _ POST ['num7'];
If (is_dir ('txt ')){
If (is_file ("txt/caculator.txt ")){
$ Open = fopen ("txt/caculator.txt", "r + ");
$ Open;
$ A = file_get_contents ("txt/caculator.txt ");
$ B = $ a. $ num;
File_put_contents ("txt/caculator.txt", $ B );
Fclose ($ open );
}
}
}
// Click the response in number 8
If ($ _ POST ['num8']) {

$ Num = $ _ POST ['num8'];
If (is_dir ('txt ')){
If (is_file ("txt/caculator.txt ")){
$ Open = fopen ("txt/caculator.txt", "r + ");
$ Open;
$ A = file_get_contents ("txt/caculator.txt ");
$ B = $ a. $ num;
File_put_contents ("txt/caculator.txt", $ B );
Fclose ($ open );
}
}
}
// Click the response in number 9
If ($ _ POST ['num9']) {

$ Num = $ _ POST ['num9'];
If (is_dir ('txt ')){
If (is_file ("txt/caculator.txt ")){
$ Open = fopen ("txt/caculator.txt", "r + ");
$ Open;
$ A = file_get_contents ("txt/caculator.txt ");
$ B = $ a. $ num;
File_put_contents ("txt/caculator.txt", $ B );
Fclose ($ open );
}
}
}
// Click the reaction of a vertex.
If ($ _ POST ['dot ']) {

$ Num = $ _ POST ['dot '];

If (is_dir ('txt ')){
If (is_file ("txt/caculator.txt ")){
$ Open = fopen ("txt/caculator.txt", "r + ");
$ Open;
$ A = file_get_contents ("txt/caculator.txt ");
$ Findimethyl = ".";
$ Pos = strpos ($ a, $ findimethyl );
If ($ pos = true ){

File_put_contents ("txt/caculator.txt", $ );
Fclose ($ open );
}
Else {
$ B = $ a. $ num;
File_put_contents ("txt/caculator.txt", $ B );
Fclose ($ open );
}
}
}
}
// Click the response of Option C.
If ($ _ POST ['clear']) {


If (is_dir ('txt ')){
If (is_file ("txt/caculator.txt ")){
$ Open = fopen ("txt/caculator.txt", "w + ");
$ Open;
$ Open = fopen ("txt/caculator2.txt", "w + ");
$ Open2;
$ Open = fopen ("txt/caculator3.txt", "w + ");
$ Open3;

}
}
}
// Click response.
If ($ _ POST ['back']) {
If (is_dir ('txt ')){
If (is_file ("txt/caculator.txt ")){
$ Open = fopen ("txt/caculator.txt", "r + ");
$ Open;
$ A = file_get_contents ("txt/caculator.txt ");
$ A = substr ($ a, 0,-1 );
File_put_contents ("txt/caculator.txt", $ );
}
}
}
// Click + response
If ($ _ POST ['add']) {
If (is_dir ('txt ')){
If (is_file ("txt/caculator2.txt") & is_file ("txt/caculator.txt") & is_file ("txt/caculator3.txt ")){
$ Open = fopen ("txt/caculator.txt", "r + ");
$ Open;
$ Open2 = fopen ("txt/caculator2.txt", "r + ");
$ Open2;
$ Open3 = fopen ("txt/caculator3.txt", "r + ");
$ Open3;
$ B = "";
$ C = "+ ";
$ A = file_get_contents ("txt/caculator.txt ");
File_put_contents ("txt/caculator.txt", $ B );
File_put_contents ("txt/caculator2.txt", $ );
File_put_contents ("txt/caculator3.txt", $ c );
}
}

}
// Click-response
If ($ _ POST ['sub']) {
If (is_dir ('txt ')){
If (is_file ("txt/caculator2.txt") & is_file ("txt/caculator.txt") & is_file ("txt/caculator3.txt ")){
$ Open = fopen ("txt/caculator.txt", "r + ");
$ Open;
$ Open2 = fopen ("txt/caculator2.txt", "r + ");
$ Open2;
$ Open3 = fopen ("txt/caculator3.txt", "r + ");
$ Open3;
$ B = "";
$ C = "-";
$ A = file_get_contents ("txt/caculator.txt ");
File_put_contents ("txt/caculator.txt", $ B );
File_put_contents ("txt/caculator2.txt", $ );
File_put_contents ("txt/caculator3.txt", $ c );
}
}

}
// Click the multiplication response.
If ($ _ POST ['mul']) {
If (is_dir ('txt ')){
If (is_file ("txt/caculator2.txt") & is_file ("txt/caculator.txt") & is_file ("txt/caculator3.txt ")){
$ Open = fopen ("txt/caculator.txt", "r + ");
$ Open;
$ Open2 = fopen ("txt/caculator2.txt", "r + ");
$ Open2;
$ Open3 = fopen ("txt/caculator3.txt", "r + ");
$ Open3;
$ B = "";
$ C = "*";
$ A = file_get_contents ("txt/caculator.txt ");
File_put_contents ("txt/caculator.txt", $ B );
File_put_contents ("txt/caculator2.txt", $ );
File_put_contents ("txt/caculator3.txt", $ c );
}
}

}
// Click the = response
If ($ _ POST ['equal']) {
If (is_dir ('txt ')){
If (is_file ("txt/caculator2.txt") & is_file ("txt/caculator.txt") & is_file ("txt/caculator3.txt ")){
$ Open = fopen ("txt/caculator.txt", "r + ");
$ Open;
$ Open2 = fopen ("txt/caculator2.txt", "r + ");
$ Open2;
$ Open3 = fopen ("txt/caculator3.txt", "r + ");
$ Open3;
$ A = file_get_contents ("txt/caculator.txt ");
$ B = file_get_contents ("txt/caculator2.txt ");
$ C = file_get_contents ("txt/caculator3.txt ");
$ Nul = "";
If ($ c = "+ "){
$ D = $ B + $;
}
If ($ c = "-"){
$ D = $ B-$;
}
If ($ c = "*"){
$ D = $ a * $ B;
}
If ($ c = "/"){
$ D = $ B/$;
}
If ($ c = "% "){
$ D = $ B % $;
}
If ($ c = "+ "){
$ D = $ a + $ B;
}
If ($ c = "√ "){
$ D = sqrt ($ );
}
File_put_contents ("txt/caculator.txt", $ d );

}
}

}

The above are most of the code. due to limited input characters, the key code is pasted out.

There are a lot of code. you should say something went wrong.

Your problem is:
If ($ _ POST ['num0']) {
Because $ _ POST ['num0'] is equal to 0, the constant condition is invalid. This branch will not enter!
Change
If ($ _ POST ['num0'] = '0 '){

Your program design is unreasonable and requires significant adjustments.
Improper use of file operation functions
Since file_get_contents and file_put_contents are used to read and write files, fopen and fclose are not required.
File_put_contents has a FILE_APPEND parameter that can be used to append content to a file without reading it and then manually appending it.

If necessary, you can use the adjusted code for your reference.

Using AJAX, and then dynamic PHP compilation, we can put a lot of companies into computing.

Your problem is:
If ($ _ POST ['num0']) {
Because $ _ POST ['num0'] is equal to 0, the constant condition is invalid. This branch will not enter!
Change
If ($ _ POST ['num0'] = '0 '){

Your program design is unreasonable and requires significant adjustments.
Improper use of file operation functions
Since file_get_contents and file_put_contents are used to read and write files, fope ......
Moderators... I just started learning...
Please give me more advice later!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.