4
How do I make a calculator in php? I made the code but does not display result.
<!DOCTYPE HTML>
<html lang = "pt-br">
<head>
<title> Exemplo</title>
<meta charset = "UTF-8">
</head>
<body>
<form action=calculadora.php" method="get" >
Primeiro Numero: <input name="num1" type="text" />
Segundo numero: <input name="num2" type="text" />
Operacao (quatro operações): <input name="operacao" type="text" />
<input type="submit" value="Calcular" />
</form>
<?php
$a = $_GET "num1";
$b = $_GET "num2";
$op =$_GET "operacao";
$c = []
if($op == "soma")
$c = $a + $b;
//echo $c = "resultado";
else if($op == "subtracao")
$c = $a - $b;
//echo $c = "resultado";
else if($op == "multiplicacao")
$c = $a*$b;
//echo $c = "resultado";
else
$c = $a/$b;
echo "O resultado da operação é: $c";
?>
</body>
</html>
Could you include the explanation of your answer? The way it is, it has become 100% dependent on external sites. If they fall, goodbye response.
– bfavaretto
It was edited for analysis.
– cavalsilva
I believe it’s best to pass the values as an argument to
calcular()
pq it is not good to use global variables ($_POST/$_GET) directly in a method.– rray