I think a very simple way to do this would be (if you do not know the language very well yet), use xampp with apache to run the files.
If you use xampp, once installed, just add your files and projects to the C: \ xampp \ htdocs \ directory and give a "start" to apache.
Well come on.
Create an html file that will have your form, for example:
<!DOCTYPE html>
<html lang="pt-br">
<head>
<title>Calculadora</title>
<meta charset="utf-8">
</head>
<body>
<form method="POST" action="calcular.php">
Primeiro número:<br>
<input name="primeiro" type="text" value="" size="30"/><br>
Segundo número:<br>
<input name="segundo" type="text" value="" size="30"/><br>
Operacao:<br>
<select name="operacao">
<option value="+">Somar</option>
<option value="-">Subtrair</option>
<option value="/">Dividir</option>
<option value="*">Multiplicar</option>
</select><br>
<input type="submit" value="Calcular"/>
</form>
</body>
</html>
Well, the "action" attribute of the form is pointing to another file, in this case a file called calculate.php is the one that will make all the logic of the calculator.
<?php
$primeiro = $_POST['primeiro'];
$segundo = $_POST['segundo'];
$operacao = $_POST['operacao'];
$resultado = 0;
if ($operacao == "+") {
$resultado = $primeiro + $segundo;
} else if ($operacao == "-") {
$resultado = $primeiro + $segundo;
} else if ($operacao == "*") {
$resultado = $primeiro * $segundo;
} else if ($operacao == "/") {
$resultado = $primeiro / $segundo;
}
echo $resultado;
?>
To add the result to an input on the same screen only using HTML , PHP , one solution would be to create only one .php file, eg calculate.php and in it you will have both the logic of the calculator and the html form, it would look something like this:
<?php
$primeiro = $_POST['primeiro'];
$segundo= $_POST['segundo'];
$operacao = $_POST['operacao'];
$resultado = 0;
if ($operacao == "+") {
$resultado = $primeiro + $segundo;
} else if ($operacao == "-") {
$resultado = $primeiro - $segundo;
} else if ($operacao == "*") {
$resultado = $primeiro * $segundo;
} else if ($operacao == "/") {
$resultado = $primeiro / $segundo;
}
?>
<!DOCTYPE html>
<html lang="pt-br">
<head>
<title>Calculadora</title>
<meta charset="utf-8">
</head>
<body>
<form method="POST" action="">
Primeiro número:<br>
<input name="primeiro" type="text" value="<?= $primeiro ?>" size="30"/><br>
Segundo número:<br>
<input name="segundo" type="text" value="<?= $segundo ?>" size="30"/><br>
Operacao:<br>
<select name="operacao">
<option value="+">Somar</option>
<option value="-">Subtrair</option>
<option value="/">Dividir</option>
<option value="*">Multiplicar</option>
</select><br>
Resultado:<br>
<input name="resultado" type="text" value="<?= $resultado ?>" size="30"/><br>
<input type="submit" value="Calcular"/>
</form>
</body>
</html>
But remember there are other solutions such as using ajax, js etc.
If you are just at the beginning of the programming world I advise you to go by the simple and go step by step learning things, as you are feeling more comfortable you can try other solutions.
But what has changed in this example compared to the first?
Come on,
I removed the value of the attribute "action" of the form, because it will point to the same page, or will be redirected by clicking "Calculate" to calulate.php again, however as you have filled in the data, you can take via POST or GET . And that way I can display the result through the tags in the HTML form.
I hope I have helped!