Php-mysql Course

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 create a highlighted bolded text?
<q> <strong> <em>
<p>Address: <strong></strong> - Tutorials.</p>
Which of these CSS codes displays the text bolded?
text-size: 18px; font-style: italic; font-weight: 800;
#id {
  font-weight: 800;
What JavaScript function can be used to call another function multiple times, to a specified time interval?
setInterval() setTimeout() push()
function someFunction() { alert(""); }
setInterval("someFunction()", 2000);
Click on the correctly defined variable in PHP.
var vname = 8; $vname = 8; $vname == 8;
$vname = 8;
echo $vname;
Simple arithmetic calculator in PHP

Last accessed pages

  1. JavaScript Course - Free lessons (25716)
  2. jsSHA - SHA Hashes and HMAC in JavaScript (1284)
  3. JavaScript Code Snippets (2002)
  4. SHA256 Encrypt hash in JavaScript (17610)
  5. Get Mouse coordinates inside a Div or an Image (13240)

Popular pages this month

  1. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (688)
  2. SHA256 Encrypt hash in JavaScript (646)
  3. Read Excel file data in PHP - PhpExcelReader (616)
  4. PHP Unzipper - Extract Zip, Rar Archives (596)
  5. Create simple Website with PHP (595)