In this exercise you will make a simple calculator using PHP.
Make a PHP web page with a form
element.
The methods must be post
, not get
.
Now make another PHP web page: This page is where you do the calculation an present the result.
Normally you use <form method="post" ...>.
Try to use method="get"
.
What difference does it make?
Use $_REQUEST or $_GET. What is the difference?
General question: How are data transported with HTTP GET and HTTP POST?
The first version of the calculator only does '+'.
Now I want more!
Add a drop down list to the form (HTML select
) with +, -, *, and /, and maybe even more operators.
Adapt the server side to handle different kinds of calculations:
if ... else ... elseif
statements.switch
statement.Refactor your PHP code so that PHP and HTML is not mixed (to much).
Put all the PHP code (except echo ...) in the to of the file.
Leave echo at the proper place of output inside the HTML
Many web programming concepts like JavaServer Faces and ASP.NET are modelled with Post back in mind: That is form elements does not refer to another page, rather to the same page.
Read about Post back in Lesson 9 : Post back in PHP.
Make a copy of your project and refactor the copy to use post back.
Make a user defined function to do the calculation.
Make a PHP class calculator.
Inspiration (the first part only. The later part i quite advanced ...)
Put the class calculator in a separate file.
Include the calculator file in the main file using the PHP syntax include or require.
Style your calculator to make it look nicer ...
Style your calculator using the Bootstrap framework.