Php code for calculator with buttons

This is my truly basic, almost style-less, pure-PHP calculator form:

<!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,'+',4,5,6,'-',7,8,9,'*','C',0,'.','/','='];
$pressed = '';
if (isset($_POST['pressed']) && in_array($_POST['pressed'], $buttons)) {
    $pressed = $_POST['pressed'];
}
$stored = '';
if (isset($_POST['stored']) && preg_match('~^(?:[\d.]+[*/+-]?)+$~', $_POST['stored'], $out)) {
    $stored = $out[0];  
}
$display = $stored . $pressed;
//echo "$pressed & $stored & $display<br>";
if ($pressed == 'C') {
    $display = '';
} elseif ($pressed == '=' && preg_match('~^\d*\.?\d+(?:[*/+-]\d*\.?\d+)*$~', $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",(count($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>

Here is a screenshot while I was testing:

Php code for calculator with buttons

You can see that I've made every button a submit button with the same name, but different values. I use a single hidden input to preserve built expression. There will be many enhancements and considerations beyond this demo, it is up to you how far to go down this rabbit hole.


P.S. For anyone who just unholstered their sidearm, squinted one eye, and sternly uttered "Hey, we don't take kindly to folks like you callin' eval() in these here parts!". Well, I've endeavored to adequately validate the string to be evaluated. If there is a known hole, please let me know and I'll try to fix it. Alternatively, this is my attempt to re-invent the eval() process with BOMDAS in mind:

Code: (Demo)

$stored = "2+3*4/6";
$components = preg_split('~([*/+-])~', $stored, 0, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE);
while (($index = array_search('*', $components)) !== false) {
    array_splice($components, $index - 1, 3, $components[$index - 1] * $components[$index + 1]);
}
while (($index = array_search('/', $components)) !== false) {
    array_splice($components, $index - 1, 3, $components[$index - 1] / $components[$index + 1]);
}
while (($index = array_search('+', $components)) !== false) {
    array_splice($components, $index - 1, 3, $components[$index - 1] + $components[$index + 1]);
}
while (($index = array_search('-', $components)) !== false) {
    array_splice($components, $index - 1, 3, $components[$index - 1] - $components[$index + 1]);
}
echo current($components);  // 4

I'm developing a PHP calculator, actually my question is when i click to any of the input buttons the value should display in the text box. below is the code what i have tried with, but im unable to get the resut. can any one help me in my work,so that how i can get the desired result.

What I have tried:

 HTML CODE
<div style="padding-left:40px">
        <h2> PHP Calculator</h2>
        <form method="post" action="">
            
            Enter value: <input type="text" name="value"> <br> <br> 
            <div style="padding-left: 105px">
                <input type="submit" value="9" name="button">
                <input type="submit" value="8" name="button">
                <input type="submit" value="7" name="button"> <input type="submit" value="+" name="button"> <br>
                <input type="submit" value="6" name="button">
                <input type="submit" value="5" name="button">
                <input type="submit" value="4" name="button"> <input type="submit" value="-" name="button" style="padding-left: 9px"> <br>
                <input type="submit" value="3" name="button">
                <input type="submit" value="2" name="button">
                <input type="submit" value="1" name="button"> <input type="submit" value="/" name="button" style="padding-left: 9px"> <br>
                <input type="submit" value="0" name="button" style="padding-left:33px"> 
                <input type="submit" value="." name="button" style="padding-right:9px"> <input type="submit" value="x" name="button" style="padding-left: 7px"> <br>
            </div> <br> 
                <input type="submit" value="Calculate" name="Calculator">
           
        </form>     
    </div>
   
   PHP CODE :
   <?php 
    
       if (isset($_POST["Calculate"]))
       {
           echo $_POST["button"];
       }
?>


This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900

How to make a calculator using PHP code?

PHP Code for Simple Calculator <? php if(isset($_POST['sub'])){ $num1=$_POST['n1']; $num2=$_POST['n2']; $oprnd=$_POST['sub']; if($oprnd=="+") $ans=$num1+$num2; else if($oprnd=="-") $ans=$num1-$num2; else if($oprnd=="x") $ans=$num1*$num2; else if($oprnd=="/") $ans=$num1/$num2; }?>

Can HTML make calculator?

To design the basic calculator, we will use HTML, CSS, and JavaScript. HTML is used to design the basic structure of the calculator. CSS styles are used to apply styles on the calculator and JavaScript is used to add the calculation functionality.