Calculator Program in PHP Using If Else

Calculator Program in PHP Using If Else. Using this program we can run the simple calculator program in browser through xampp. Calculating machine is another name for it. Small electrical or mechanical device that conducts computations and necessitates personal intervention for each operation a person who is in charge of such a machine. The goal of a calculator is to perform accurate calculations quickly. It is obvious that, to the extent possible, a calculator should relieve the user of the need to perform mental calculations and rely on paper.

<!DOCTYPE html>

<title>Welcome to</title>

$a = $_POST['a'];
$b = $_POST['b'];
$operator = $_POST['operator'];
$result = '';
if (is_numeric($a) && is_numeric($b)) {
switch ($operator) {
case "Add":
$result = $a + $b;
case "Subtract":
$result = $a - $b;
case "Multiply":
$result = $a * $b;
case "Divide":
$result = $a / $b;


<div id="page-wrap">
<form action="" method="post" id="quiz-form">
<input type="number" name="a" id="a" required="required" value="<?php echo $a; ?>" /> <b>First number</b>
<input type="number" name="b" id="b" required="required" value="<?php echo $b; ?>" /> <b>Second number</b>
<input readonly="readonly" name="result" value="<?php echo $result; ?>"> <b>Result</b>
<input type="submit" name="operator" value="Add" />
<input type="submit" name="operator" value="Subtract" />
<input type="submit" name="operator" value="Multiply" />
<input type="submit" name="operator" value="Divide" />


 First number

 Second number



Uses of GET and POST in Calculator Program in PHP

The POST method uses HTTP headers to send data. The data is encoded as specified for the GET method and stored in the QUERY STRING header.

The data size that can be sent with the POST method is unrestricted. Both ASCII and binary data can be sent using the POST method.

The HTTP request methods Get and Post are used inside the form> tag to submit form data to the server.

The HTTP protocol allows communication between the client and the server, with the client being a browser and the server being a program operating on the computer system that hosts your website.

Wrap Keyword in PHP

When a string reaches a certain length, the wordwrap() method wraps it into new lines.

Note that this function may result in white spaces at the start of lines.

The wordwrap() function wraps a string into a predetermined amount of characters.

Using a string break character, wraps a string to a specified amount of characters.

The string that will be entered. The length of the string at which it will be wrapped.

Create Simple Button in Calculator Using HTML and CSS

Definition and Application The submit button is defined by the input type=”submit”>, which submits all form values to a form-handler. In most cases, the form-handler is a server page with a script for processing the submitted data. The form’s action element specifies the form’s handler. This is simple php program for calculator to understand how to implement in php.