Categories: BlogIT

create calculator in php || Simple Calculator Using PHP

Below mention Code if you like it so please share this post every one .
php
<!DOCTYPE html>
<html>
<head>
<title>Responsive Calculator in PHP</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
.container {
display: flex;
flex-wrap: wrap;
justify-content: center;
align-items: center;
height: 100vh;
}
input[type="number"], select {
padding: 10px;
margin: 10px;
width: 100%;
border: 1px solid #ccc;
border-radius: 4px;
box-sizing: border-box;
}
button[type="submit"] {
padding: 10px;
margin: 10px;
width: 100%;
background-color: #4CAF50;
color: white;
border: none;
border-radius: 4px;
cursor: pointer;
}
button[type="submit"]:hover {
background-color: #45a049;
}
</style>
</head>
<body>
<div class="container">
<form method="post">
<label for="num1">Enter Number 1:</label>
<input type="number" id="num1" name="num1" placeholder="Enter Number 1" required>

<label for="num2">Enter Number 2:</label>
<input type="number" id="num2" name="num2" placeholder="Enter Number 2" required>

<label for="operator">Select Operator:</label>
<select id="operator" name="operator" required>
<option value="+">Addition</option>
<option value="-">Subtraction</option>
<option value="*">Multiplication</option>
<option value="/">Division</option>
</select>

<button type="submit" name="submit">Calculate</button>
</form>

<?php
if(isset($_POST['submit'])) {
$num1 = $_POST['num1'];
$num2 = $_POST['num2'];
$operator = $_POST['operator'];
$result = '';

switch($operator) {
case '+':
$result = $num1 + $num2;
break;
case '-':
$result = $num1 - $num2;
break;
case '*':
$result = $num1 * $num2;
break;
case '/':
$result = $num1 / $num2;
break;
default:
$result = '';
}

if($result !== '') {
echo '<h2>Result:</h2>';
echo '<p>' . $num1 . ' ' . $operator . ' ' . $num2 . ' = ' . $result . '</p>';
}
}
?>


</div>
</body>
</html>

This code creates a basic responsive calculator form using HTML and CSS, and uses PHP to handle the form submission and perform the calculation. The form includes two input fields for entering the numbers to be calculated, a select field for selecting the operator, and a submit button to perform the calculation. When the user submits the form, PHP code handles the form data and performs the appropriate calculation based on the selected operator, and displays the result on the page.

#PHP #HTML #CSS #calculator #responsive-design #form-validation #arithmetic-operations #switch-case #web-development
rasiblog213i

Recent Posts

pregnancy ke seventh ke tips and care || प्रेगनेंसी का सातवां महीना — लक्षण, आहार, शारीरिक परिवर्तन और शिशु का विकास

प्रेगनेंसी का सातवां महीना आपकी जिंदगी में खुशियां भर देता है। इस दौरान आपके घर…

4 weeks ago

NPCI implemented standard reward rates for RuPay credit cards on UPI transactions.

Customers who use their RuPay credit cards to make payments on UPI (Unified Payments Interface) and other…

1 month ago

Mahindra Group appoints Ankit Todi as Group Chief Sustainability Officer

The Mahindra Group has announced the appointment of Ankit Todi as the new Group Chief…

2 months ago

Anticipating GPT-5: What We Know So Far from Sam Altman’s Hints

Introduction: In a recent statement, OpenAI CEO Sam Altman has sparked excitement and curiosity in…

2 months ago

Pregnancy Side Effects 101: What Every Expectant Mother Should Know

Pregnancy can bring about a variety of side effects due to the significant physical, hormonal,…

3 months ago