Friday, December 12, 2008

PHP Calculator

For making a calculator in php which performs basic function like addition, subtraction, division and multiplication, PHP has to be installed on your system.

At first open the notepad to write the html code for creating layout of the calculator. We want two textboxes to put two inputs on which function is to be performed like addition, subtraction and others. And we need 4 buttons naming Add, Subtract, Multiply and Divide. And title we want to give to this is “Calculator”.

So the code of this html is:

Give title to the code"Calculator.”

And in tag give Calculator as Heading. Then in body tag, write a form tag which calls ‘calculator.php ‘file.Use the "post method”.

Then make two textboxes, giving its input type as text and name as i1 and i2 respectively.





Make 4 buttons of input type Submit and write in name and value: ADD, Subtract, Multiply and Divide. Close the form tag and body and html tag too. Save this html file in "htdocs" folder of your server, I saved it in Xampp as I am using this server. Then execute this in browser by writing http://localhost/calculator.html

This will display the calculator form which contains two input textboxes and 4 buttons as shown above in the window..


Now next part is to write Php code as this html file will call php file, so write the code in the notepad and save it as "Calculator.php" in the 'htdocs' folder of Xampp. Code of this php file is:



Heading “solution” is given as this page will be the solution of any function performed. Then php tag is started. Six variables are assigned the Request method which will request the particular function. Then if loop is started ,if function is of ADD, then input 1 and input 2 are added and are inserted in result variable. Printf statement is used which will print the result of the addition of input 1 and input 2. Else loop is used when function is other then of addition.

Like above else is used if function is of subtraction. Same is the case here after subtracting the two inputs, they are inserted into an variable naming “result”. Then using printf statement, result is printed. Difference between all these are only their signs, like for addition + are used, for subtraction ‘-‘is used.

At the end, a href tag is used to display a link on the solution page, which when clicked will open the previous calculator page. And body and html tags are closed.

Now we will provide two inputs in the textboxes and will perform different functions on them.


Two inputs 458 and 24 are provided in two textboxes. Now we will perform addition, subtraction and other functions also. By clicking Add button, 'calculator.php' file will be called and a solution page will open which will give the result after addition and a link named” Return to the Calculator"  which opens the previous page.



 

In the same manner others are performed. We will look applying other functions on these two inputs only.







This way multiplication, division and subtraction are performed. This way a PHP calculator is prepared with the help of html and php code, and this is the simplest calculator in design.


No comments:

Post a Comment