<html>
<head>
<title>Taschenrechner</title>
</head>
<body>
<form name="taschenrechner" method="post" action="test.php">
<input name="zahl1" type="text" id="zahl1">
<select name="opt" id="opt">
<option value="+">+</option>
<option value="-">-</option>
<option value=":">/</option>
<option value="*">*</option>
</select>
<input name="zahl2" type="text" id="zahl2">
<input type="submit" name="Submit" value="Abschicken">
</form>
<?php
$zahl1 = $_POST["zahl1"];
$zahl2 = $_POST["zahl2"];
$opt = $_POST["opt"];
$zahl1 = str_replace (",",".",$zahl1);
$zahl2 = str_replace (",",".",$zahl2);
if(!is_numeric($zahl1) || !is_numeric($zahl2)){
echo "Bitte geben Sie die Zahlen korrekt an!";
}
else{
switch ($opt) {
case "+":
$ergebnis = $zahl1 + $zahl2;
break;
case "-":
$ergebnis = $zahl1 - $zahl2;
break;
case ":":
$ergebnis = $zahl1 / $zahl2;
break;
case "* ":
$ergebnis = $zahl1 * $zahl2;
break;
}
echo" $zahl1 $opt $zahl2 = <b>$ergebnis</b><br>";
echo str_replace (".",",",$ergebnis)."<br>";
if(ereg(".[0-9]{3,}$", $ergebnis)) {
echo number_format($ergebnis,2);
}
}
?>
</body>
</html>