42
loading...
This website collects cookies to deliver better user experience
Prerequisites: HTML and CSS knowledge is helpful but not mandatory
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Calculator</title>
</head>
<body>
<h1>Calculator World</h1>
<form>
<input type="text" placeholder="operand 1">
<select>
<option selected>None</option>
<option>Add</option>
<option>Subtract</option>
<option>Multiply</option>
<option>Divide</option>
</select>
<input type="text" placeholder="operand 2">
<button>=</button>
<div>
</div>
</form>
</body>
</html>
*{
box-sizing: border-box;
padding: 0;
margin: 0;
}
body {
display: flex;
height: 100vh;
flex-direction: column;
justify-content: center;
align-items: center;
background: lightgray;
}
h1 {
margin-top: -10%;
margin-bottom: 26px;
font-size: 3.8rem;
font-family: monospace;
}
form {
display: flex;
width: 80%;
justify-content: space-evenly;
align-items: center;
background: whitesmoke;
border-radius: 30px;
}
form input, button, select {
padding: 20px;
font-size: 1.2rem;
border: none;
background: inherit;
}
form input:focus {
outline: 1px solid dimgray;
}
form button {
background: dimgray;
color: whitesmoke;
border-radius: 50%;
}
div {
color: dimgray;
font-weight: 900;
font-size: 1.2rem;
display: flex;
justify-content: center;
align-items: center;
}
@media screen and (max-width: 600px) {
h1 {
font-size: 3rem;
}
form {
width: 95%;
padding: 0 5%;
}
form input, button, select {
width: 100%;
margin: 0 5px;
border-radius: 20px;
}
form button {
/*border-radius: 0;*/
/*padding-left: 20px;*/
/*padding-right: 0;*/
width: 10%;
}
}
<link rel="stylesheet" href="./style.css">
<?php ?>
. Anything written within those tags is regarded as valid PHP code. Be sure to save your file with the .php
file extension.=
button. So within our form, just after the button element, we will write some PHP code to calculate user inputs as requested.<input type="text" placeholder="operand 1" name="one">
<select name="operator">
...
<input type="text" placeholder="operand 2" name="two">
<button name="equals">=</button>
POST
or GET
. We will use the latter in our code. Then we need to set an action attribute that tells the browser which file will process the form it's sending, in this case, it's going to be the same file. My file is named index.php
. If you named your file differently, be sure to check that it matches with the value provided in the action attribute. See the example below.<form action="index.php" method="GET">
<div>
<?php
if(isset($_GET['equals'])){
$operandOne = $_GET['one'];
$operandTwo = $_GET['two'];
switch ($_GET['operator']){
case 'None':
echo 'Select and operator';
break;
case 'Add':
echo $operandOne + $operandTwo;
break;
case 'Subtract':
echo $operandOne - $operandTwo;
break;
case 'Multiply':
echo $operandOne * $operandTwo;
break;
case 'Divide':
echo $operandOne / $operandTwo;
break;
default:
echo 'Nothing was supplied';
break;
}
}else {
echo '0';
}
?>
</div>
<?php
Starts the PHP tag so anything we write within it is regarded as valid PHP code.if(isset($_GET['equals'])){
Checks if the button, which has the name 'equals' has been clicked$operandOne = $_GET['one'];
This statement declares a variable and initializes its value to whatever got passed in by the user into the first input field with the name 'one'.$operandTwo = $_GET['two'];
Creates a variable and initializes its values to what got passed into the second input field with name 'two'.switch ($_GET['operator']){
A switch statement on what option got selected by the user on the select element.case 'None':
This line says if the user selected None, perform the following action.echo 'Select and operator';
This string gets printed on the pagebreak;
Ends the switch statementcase 'Add':
If the user selects Add, perform the following action.echo $operandOne + $operandTwo;
Add both values and print the sum on the pagebreak;
Ends the switch statementcase 'Subtract':
If the user selects Subtract, perform the following action.echo $operandOne - $operandTwo;
Subtract the second value from the first and print the result on the pagebreak;
Ends the switch statementcase 'Multiply':
If the user selects Multiply, perform the following action.echo $operandOne * $operandTwo;
Multiply both values and print the result on the pagebreak;
Ends the switch statementcase 'Divide':
If the user selects Divide, perform the following action.echo $operandOne / $operandTwo;
Divide the second value from the first and print the result on the pagebreak;
Ends the switch statementdefault:
If none of the above happens, do the followingecho 'Nothing was supplied';
Prints the stringbreak;
End the program}
Ends Switch block}else {
Else statementecho '0';
Prints string to the screen until the if statement evaluates to true}
Ends the Else statement?>
Closes the PHP tag