Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Simple PHP calculator
<!DOCTYPE html>
<html>
<head>
<title>Calculator</title>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<div class="container" style="margin-top: 50px">
<?php
// If the submit button has been pressed
if(isset($_POST['submit']))
{
// Check number values
if(is_numeric($_POST['number1']) && is_numeric($_POST['number2']))
{
// Calculate total
if($_POST['operation'] == 'plus')
{
$total = $_POST['number1'] + $_POST['number2'];
}
if($_POST['operation'] == 'minus')
{
$total = $_POST['number1'] - $_POST['number2'];
}
if($_POST['operation'] == 'times')
{
$total = $_POST['number1'] * $_POST['number2'];
}
if($_POST['operation'] == 'divided by')
{
$total = $_POST['number1'] / $_POST['number2'];
}
// Print total to the browser
echo "<h1>{$_POST['number1']} {$_POST['operation']} {$_POST['number2']} equals {$total}</h1>";
} else {
// Print error message to the browser
echo 'Numeric values are required';
}
}
?>
<!-- Calculator form -->
<form method="post" action="calculator.php">
<input name="number1" type="text" class="form-control" style="width: 150px; display: inline" />
<select name="operation">
<option value="plus">Plus</option>
<option value="minus">Minus</option>
<option value="times">Times</option>
<option value="divided by">Divided By</option>
</select>
<input name="number2" type="text" class="form-control" style="width: 150px; display: inline" />
<input name="submit" type="submit" value="Calculate" class="btn btn-primary" />
</form>
</div>
</body>
</html>
@ghost

This comment has been minimized.

Copy link

commented Jan 9, 2018

https://github.com/akash-mahanty/calculator
IS THE BEST CALCULATOR AVAILABLE ON GITHUB
cal

@GMD12

This comment has been minimized.

Copy link

commented Aug 10, 2018

nice

@myust25

This comment has been minimized.

Copy link

commented Feb 6, 2019

Nice,

@ShujaShah

This comment has been minimized.

Copy link

commented Jul 28, 2019

Nice Ui

@roof-skills

This comment has been minimized.

Copy link

commented Aug 12, 2019

how to genrated

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.