PHP: An Essential Beginners Guide to Learn the Realms of PHP From A-Z by Pratt Logan

PHP: An Essential Beginners Guide to Learn the Realms of PHP From A-Z by Pratt Logan

Author:Pratt, Logan [Pratt, Logan]
Language: eng
Format: epub
Published: 2020-01-18T16:00:00+00:00


Chapter Five: First Lab: A Calculator

As the fourth chapter points out, it's time to take stock. We've already seen a lot of the basics of PHP together: with everything we've seen, you can do great things. In the second part of this tutorial, we will put everything into practice. But before, we will do practical work so that you can judge for yourself the state of your knowledge.

This lab is also the last chapter of Part I, so be careful.

Objectives

The purpose of this TP is that you realize a calculator. But don't worry, you're going to make it a very simple one. First, because you don't all have enough math skills to do something more complicated.

Secondly, and mainly because you are already going to drool to do something simple, I'm sure very few of you will succeed in doing something that works perfectly.

But don't worry, this lab is difficult, it's voluntary. I want you to understand for yourselves that you are not far away. You have seen the basics, but you have not practiced much yet. You therefore cruelly lack experience, and as the saying goes: "It is by forging that one becomes a blacksmith. "

So do not be surprised if you wade through semolina throughout this lab.

This calculator will be very simple. It will consist of a form with two small text boxes and a drop-down list. The textboxes will be used by the visitor to enter the two numbers he wants to process. The user will be asked to enter only whole numbers. We could take floating-point numbers, but I don't want to (and that doesn't change anything anyway!).

The drop-down list will give access to five operations, the basic operations:

addition; subtraction ; division; multiplication; modulo.

Once the visitor has entered the numbers and the type of operation, your script should display the calculation and the answer.

Pay attention to the case where the visitor would like to divide (or do the modulo) by zero. Indeed, in mathematics, you cannot divide a number by zero; it gives an indefinite result. It's the same in PHP (and an error will be displayed).

You will, therefore, need to check the numbers that the visitor will enter. If he wants to divide or make the modulo by zero, you will have to display a message: an error has occurred: you cannot divide by zero. However, there is a second thing you need to check. Some browsers, via extensions, allow you to modify forms (all HTML source code, even!). We can, therefore, modify the value and company attributes. Therein lies the risk. To identify which operation to do, you will put special values ​​ in the value attributes. You will, therefore, have to check that you receive one of these values, otherwise, you will have to display the message: an error has occurred: operation undefined.

If you display one of these two errors, you should not display the calculation and the answer.

But in any case, you will always display the form so that other calculations can be made.



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.