Build a Calculator with PHP OOP and Procedural Error Handling (Simple Method)

Ayobami Alli
4 min readMar 2, 2022

This code write-up is just a simple and beginner way of building a calculator combining both OOP and Procedural knowledge.

Therefore, it is particularly for php programmers transitioning from procedural to object oriented programming. I will do my best to explain the process as well.

Steps we would need to follow as we create this calculator?

1. An HTML Form template to just show the:

a. Numbers

b. Signs (+, _, /, *)

c. Submit button

d. Answer Space

2. We will have to php codes that accepts out datas from this template

3. Create an object called “calculation”, which would have function add, minus, multiply and divide

4. We would make sure our functions are called/used so we can get adequate answers.

1. Creating our Calculator HTML Template

Create a folder and name it “calculator”, make sure you are creating it inside your local server proper file directory. Open your folder with your code-editor and create a file, named “index.php”.

The Code Below has a mix of both HTML and CSS, but I am going to focus on breaking down the steps of the HTML for now.

a. The HTML head section: I added bootstrap cdn just to add a bit of style to our html

b. The Body;

i. Form opening with method as POST and action as blank “”. Remember we want to generate our result in this same page

ii. Container

iii. Number input type for our first number

Iv. Select option for our signs

v. Number input type for our second number

vi. Submit button

v. A text Area where our answer will appear.

2. Php Codes that accepts our data’s

Remember we have chosen the POST method for our form and nothing “” for the form action. Which means we can actually obtain our datas in this same page.

Obtaining our datas?

Check the code below and notice where I placed it. Yeah! Inside the textarea where we want our answers printed.

What do we have there?

if(isset($_POST[‘submit’]))

If our variable submit is set and clicked

$num1=$_POST[‘num1’];

$symbol=$_POST[‘symbol’];

$num2=$_POST[‘num2’]

Post the datas entered by users inside the given variables $num1, $symbol, $num2

if($num1 == “” || $symbol == “” || $num2 == “”){

In case or If any of the variables is empty, echo “insert missing field”

Else if the symbol is empty or not chosen echo “please select an operator”. And the rest could be explained as thus: assuming the symbol selected is none, “please select an operator”, else if it is “add”, from this instantiated object stored in this variable, go to the function called add and pass these two parameters ($num1, $num2).

elseif($symbol == “add”){

$calc->add($num1, $num2);

Now let’s talk about creating an object with the function of add, minus, multiply, divide

3. Create a new file named “backend.php”

This is where we would define the functions that performs the operation we have decided to exhibit

class Calculation{

Here we create a class called “Calculation” then we create the functions as shown below for example:

public function add($num1, $num2)

{ echo ($num1+$num2);

}

Once all that is set, we must make sure we include this object in our index.php file and instantiate it too:

include “backend.php”;

$calc=new Calculation(); //Instantiating

Remember this?

elseif($symbol == “add”){ //$calc is the instance of our class or object

$calc->add($num1, $num2);

What this means is so long as we’ve included our object file, we can create an instance of it, which we can then use as a pointer ~> to the function or method we want to use.

Thanks for your time! Keep being motivated!

In the meantime, you can get the full source code from my git repo by just clicking the link below.

https://github.com/AlliAyobami/Oop-Calculator.git

--

--

Ayobami Alli

Ayobami Alli is a writer and a programmer and with experience and certification in PHP and MySQL.