<!DOCTYPE html>
<html>
<head>
<title>Calculadora em php</title>
</head>
<body>
<?php
if(isset($_POST['acao']) && $_POST['acao']=='calcular'):
$n1 = filter_input(INPUT_POST, 'n1');
$n2 = filter_input(INPUT_POST, 'n2');
$tipo = filter_input(INPUT_POST, 'tipo');
if($n1 == '' || $n2==''):
echo 'Informe os numeros para o calculo!';
elseif( !is_numeric($n1) && !is_numeric($n2) ):
echo 'São aceitos apenas numeros , tente novamente';
else:
if($tipo=='+'):
$calcular = $n1 + $n2;
elseif($tipo == '-'):
$calcular = $n1 - $n2;
elseif($tipo == '/'):
$calcular = $n1 / $n2;
else:
$calcular = $n1 * $n2;
endif;
echo $calcular;
endif;
endif;
?>
<form action="" method="post">
<input type="text" name="n1" value="<?= (isset($_POST['n1']) ? $_POST['n1'] : '' ) ?>" />
<input type="text" name="n2" value="<?= (isset($_POST['n1']) ? $_POST['n2'] : '' ) ?>">
<select name="tipo">
<option value="+">+</option>
<option value="-">-</option>
<option value="/">/</option>
<option value="*">*</option>
</select>
<input type="submit" name="acao" value="calcular"/>
</form>
</body>
</html>
Sign up here with your email
ConversionConversion EmoticonEmoticon