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.
- Click on the code to select it.
$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">
<meta charset="utf-8" />
<title>Arithmetic calculator</title>
<?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>
<input type ="number" name="nr2" id="nr2" step="any" value=""><br>
<input type ="submit" value="Calculate">
- Results this form:

Daily Test with Code Example

Which tag is used to add definition lists into a <dl> element?
<dt> <dd> <li>
  <dd> - Hyper Text Markup Language</dd>
  <dd> - Language for web pages</dd>
Which CSS property can hide an element on page, letting an empty space in its place?
display position visibility
#id {
  visibility: hidden;
Click on the event which is triggered when the mouse clicks on an object.
onclick onmouseover onfocus
document.getElementById("id").onclick = function(){
Indicate the PHP variable that contains the contents of both $_GET, $_POST, and $_COOKIE arrays.
if(isset($_REQUEST["id"])) {
  echo $_REQUEST["id"];
Simple arithmetic calculator in PHP

Last accessed pages

  1. File Handling with fopen (2242)
  2. PHP PDO - Select query, fetch (18634)
  3. Draw arrow markers with clicks in html element (1114)
  4. Remove / Get duplicate array values - Reset array keys in PHP (7908)
  5. JavaScript code and PHP (17336)

Popular pages this month

  1. Qwop (2963)
  2. Get Attribute (ID, Class, Name, Title, Src) with jQuery (2316)
  3. Get the value of the selected /checked checkboxes in a form (1464)
  4. Insert, Select and Update NULL value in MySQL (1302)
  5. Add, Change, and Remove Attributes with jQuery (1208)