Skip to content

Instantly share code, notes, and snippets.

@rapisenpai
Last active September 9, 2022 12:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rapisenpai/48cc804cb2343ae75b8e29abc6c1a0ea to your computer and use it in GitHub Desktop.
Save rapisenpai/48cc804cb2343ae75b8e29abc6c1a0ea to your computer and use it in GitHub Desktop.
Write a PHP code that will ask for 2 integers and the operator to perform mathematical operations.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>PHP Operators</title>
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Poppins:ital,wght@0,100;0,200;0,300;0,400;0,500;0,600;0,700;0,800;0,900;1,100;1,200;1,300;1,400;1,500;1,600;1,700;1,800;1,900&display=swap" rel="stylesheet">
<style>
*{
font-family: 'Poppins', sans-serif;
}
#input {
height: 40px;
border: black solid 1px;
border-radius: 3px;
margin: 5px;
width: 300px;
}
#menu {
margin-top: 5px;
font-size: 20px;
padding: 2px;
}
button {
background-color: #04AA6D;
border: none;
color: white;
font-size: 20px;
border-radius: 5px;
margin-top: 5px;
padding: 7px 20px 7px 20px;
}
button:hover {
background-color: #059862;
}
input {
text-align: center;
font-size: 17px;
}
.main {
display: flex;
margin: 0 auto;
background-color: #04AA6D;
max-width: 700px;
margin-top: 50px;
padding: 20px;
border-radius: 10px;
background-color: #F2F2F2;
}
label {
display: inline-block;
width: 200px;
}
.forms {
display: block;
margin: 0 auto;
}
</style>
</head>
<body>
<!-- <p>Write a PHP code that will ask for 2 integers and the operator to perform mathematical operations.</p> -->
<div class="main">
<div class="forms">
<form>
<label for="num1">Enter 1st number:</label>
<input id="input" type="text" name="num1">
<br>
<label for="operators">Enter operator:</label>
<input id="input" type="text" name="operators">
<br>
<label for="num2">Enter 2nd number:</label>
<input id="input" type="text" name="num2">
<br>
<button name="submit" value="submit" type="submit">Total</button>
</form>
<?php
if (isset($_GET['submit'])) {
$num1 = $_GET['num1'];
$num2 = $_GET['num2'];
$operator = $_GET['operators'];
if (!is_numeric($num1) || !is_numeric($num2)) {
echo "<br>Invalid number";
}
elseif (empty($num1) || empty($num2)) {
echo "<br>Please enter a number";
} else {
switch ($operator) {
case "+":
echo "<p>" . "Answer is: " . $num1 + $num2 . "</p>";
break;
case "*":
echo "<p>" . "Answer is: " . $num1 * $num2 . "</p>";
break;
case "x":
echo "<p>" . "Answer is: " . $num1 * $num2 . "</p>";
break;
case "/":
echo "<p>" . "Answer is: " . $num1 / $num2 . "</p>";
break;
case "-":
echo "<p>" . "Answer is: " . $num1 - $num2 . "</p>";
break;
case "%":
echo "<p>" . "Answer is: " . $num1 % $num2 . "</p>";
break;
case "**":
echo "<p>" . "Answer is: " . $num1 ** $num2 . "</p>";
break;
default:
echo "<br>Please enter a valid operator or number";
}
}
}
?>
</div>
</div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment