<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Basic PHP Calculator</title>
</head>
<body>
<?php
//var_export($_POST);
//echo "<br>";
$buttons=[1,2,3,&#039;+&#039;,4,5,6,&#039;-&#039;,7,8,9,&#039;*&#039;,&#039;C&#039;,0,&#039;.&#039;,&#039;/&#039;,&#039;=&#039;];
$pressed=&#039;&#039;;
if(isset($_POST[&#039;pressed&#039;]) && in_array($_POST[&#039;pressed&#039;],$buttons)){
    $pressed=$_POST[&#039;pressed&#039;];
}
$stored=&#039;&#039;;
if(isset($_POST[&#039;stored&#039;]) && preg_match(&#039;~^(?:[\d.]+[*/+-]?)+$~&#039;,$_POST[&#039;stored&#039;],$out)){
    $stored=$out[0];    
}
$display=$stored.$pressed;
//echo "$pressed & $stored & $display<br>";
if($pressed==&#039;C&#039;){
    $display=&#039;&#039;;
}elseif($pressed==&#039;=&#039; && preg_match(&#039;~^\d*\.?\d+(?:[*/+-]\d*\.?\d+)*$~&#039;,$stored)){
    $display.=eval("return $stored;");
}

echo "<form action=\"\" method=\"POST\">";
    echo "<table style=\"width:300px;border:solid thick black;\">";
        echo "<tr>";
            echo "<td colspan=\"4\">$display</td>";
        echo "</tr>";
        foreach(array_chunk($buttons,4) as $chunk){
            echo "<tr>";
                foreach($chunk as $button){
                    echo "<td",(sizeof($chunk)!=4?" colspan=\"4\"":""),"><button name=\"pressed\" value=\"$button\">$button</button></td>";
                }
            echo "</tr>";
        }
    echo "</table>";
    echo "<input type=\"hidden\" name=\"stored\" value=\"$display\">";
echo "</form>";
?>
</body>
</html>

Add a code snippet to your website: www.paste.org