PHP Snippet 1:
<?php
$res = 0;
$mathExpression = "((2+2) /4) *5";
eval("\$res = $mathExpression;");
echo $res;
?>
PHP Snippet 2:
<form method ="POST" action="<?php ($SERVER["PHP_SELF"]);?>">
PHP Snippet 3:
<form method ="POST" action="<?php echo $_SERVER["PHP_SELF"];?>">
PHP Snippet 4:
<button type="submit" name="number6" value="6" style="width:20px; padding-left:5px; margin-left:2px;">6</button>
PHP Snippet 5:
<input id='result' type="text" width="50" height="20" name="display" value="<?php echo $display or $newdisplay or $answer;?>">
PHP Snippet 6:
<button type="button" name="number6" value="6" onClick="document.getElementById('result').value += this.value">6</button>
PHP Snippet 7:
<?php
$number1 = (int)($_POST['number1']);
$number2 = (int)($_POST['number2']);
$action = $_POST['action'];
if(!isset($number1)){
echo 'Missing first operand!';
}
else if(!isset($number2)){
echo 'Missing second operand!';
}
else if(!empty($action)){
if($action == "Add"){
$result = $number1 + $number2;
}
else if($action == "Subtract"){
$result = $number1 - $number2;
}
else if($action == "Multiply"){
$result = $number1 * $number2;
}
else if($action == "Divide"){
$result = $number2 != 0 ? $number1 / $number2 : "NaN";
}
if(isset($result)){
echo "Your result is: ".$result;
}
}
?>
<form action="" method="POST">
<input type="number" required="required" name="number1" />
<input type="number" required="required" name="number2" />
<input type="submit" name="action" value="Add" />
<input type="submit" name="action" value="Subtract" />
<input type="submit" name="action" value="Multiply" />
<input type="submit" name="action" value="Divide" />
</form>