04 October 2017

arithmetic calculator in PHP

The code from this page shows how to make a simple arithmetic calculator in PHP with the basic operators (addition, subtraction, multiplication, division), using a html form and a simple php script.

 data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAOEAAADhCAMAAAAJbSJIAAAAY1BMVEVWpeH///9Ro+BCnd+Dvenp8/vY6vhys+ZssOTS5vaRwepOouD4+/1zteZZp+L1+v2AueeZx+xkrOPF3/Tl8fq62fJfquOw0/Clze7h7vmYxuy31/Hv9/zN4/WLv+l6ueeo0O87w15qAAAHSklEQVR4nO2d25qiOhCFIS2IBoKEDiAH5f2fcqO2Y1IV3RfNoWbvWhczF6vbj78TMNQhCQIWi8VisVgsFovFYrFYLBaLxWKxWCwWi57EbNqaxCMhZKCqZCZppQJJilNIfSmMKYfDPBqM6dpMSSqMQiT1IcrDeZWfh6KSW7PdJdT4nc6M9wMZtxSmqsy+F8G7Ky3V1sMoRLEc301Rtu0oCtUtCzgNYxtsyChUvcwdaCsv1HaIqugXBwzD/rIZoThFKwCG4SHZCFHochXAMKzVRoSXlQDDfpsHqlDDWoRht8kgimw1wLCvthhEadYjDDd5nH6t8yB9qNxg8SaqFQHD6GsDwh26ivE4k1oD38VSCoRRpmaKYUihWrgapEB4mfFWERIuJggQzjuP0N/vv0cI10tMuIAWJrz+5wnhkpAJFxAT/vLTmXAFMeEvP50JVxAT/vLTmXAFMeEvP50JVxAT/vLTmXAF/f8Iw+ucgXdZEySMk5kCwjedYPqcAmEYF5fdPDqOqEaHBGGYRjPJU/9Ag3BJMSETMiETMiETMiETMiETMiETMiETMiETMiETOkpj0zSNif2XmR66phnN2e/mn1wqhIdjoqQU1f7oa0ocnm7hc8tdosTkZo2vRJ4IodHi3vQ5/ZvgjqFaPzpCJ3ePm1Ga6ukG1wNVQmN1CooKIta2i1qmCqsfViR4lpMgPDh9giJxR6J0XJm5t1vntFHKK5qoFAjznZt8koXdUhBBd7Sr8c+gdUui1lQKhDFo3HHn2gDdvf20MdDNKOaeRpg/FNa9ljb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J8XAlc4QD+C0I0nyjBJcjW8F29L6U61+CqPJNGv1cf7IjnmjA5uc3BPKCHzBSAcFQpyZsZ4X+OQ0O+j7l2RmPnYFeQiPfx0hOitIWuc/5niW2mOI81YU84fmY/8hfNI4DDnOTFG8D7/BcaFuD2kMXaeHdIAuyR5SmCIUzuIzBylC4WSXepi3QlF/CoThOXPWZaBb212XSRDYd/NWdm8mJcJpZfJCFCi9MuiXK1HHvbHmqSe5RoTQej8Se5xeibOnG+wwwmsb4uBCeQ9as68qVekMVebdlNaJvrl7/0HQ4+RWlT55m4bIEIZ9aTpzeBeomFxjDu+qZKO76y8+3YJQv7nORdRvQBh8LX8k90uwaG4VoaKeJYUKbtbQmgHTVMOF6yqE+t0TY36ZjQ7mhrv7LyZcFbeO3MXzkjKbHFp9E9oXYBmd0cv1WsK7PS2ivNiIL/CXZ80up1xsfcT9m93lZpTZEtDbGTGzarEp4IT49aZPax71ly0WpEDy8i6r+Xu+MtlitYYkq2KI5h/I/Fxego1n6FNCVrvGDPGcGkyRBSQG8KFbXjfZzymtJJHxe2meg57+aGscFovFYrFYLBaLxWKxWCwWi8VisVgsFgvpHwLsj/l+ZajEAAAAAElFTkSuQmCC
  • <?php
    $res =''; //store the outputed value (added in html)
    
    //if required form data submitted
    if(isset($_POST['nr1']) && isset($_POST['nr2']) && isset($_POST['opr'])){
      //get form data
      $nr1 = floatval($_POST['nr1']);
      $nr2 = floatval($_POST['nr2']);
      $opr = trim(strip_tags($_POST['opr']));
    
      //calculate according to $opr
      if($opr =='+') $res = $nr1 + $nr2;
      else if($opr =='-') $res = $nr1 - $nr2;
      else if($opr =='*') $res = $nr1 * $nr2;
      else if($opr =='/') $res = $nr1 / $nr2;
    
      $res = $nr1 .' '. $opr .' '. $nr2 .' = '. number_format($res, 2);
    }
    ?>
    <!doctype html>
    <html lang="en">
    <head>
    <meta charset="utf-8" />
    <title>Arithmetic calculator</title>
    </head>
    <body>
    <?php echo $res; ?>
    <form action="" method="post">
    <input type ="number" name="nr1" id="nr1" step="any" value="0">
    <select name="opr" id="opr">
    <option value="+">+</option>
    <option value="-">-</option>
    <option value="*">*</option>
    <option value="/">/</option>
    </select>
    <input type ="number" name="nr2" id="nr2" step="any" value=""><br>
    <input type ="submit" value="Calculate">
    </form>
    </body>
    </html>
    

Start typing and press Enter to search