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

What attribute makes an option from <select> selected?
checked="checked" selected="selected" disabled="disabled"
<select name="a_name">
 <option value="val1">Option 1</option>
 <option value="val2" selected="selected">Option 2</option>
What CSS value allows to create color gradients for background?
contain repeat-x linear-gradient
#id {
  background: linear-gradient(top left, #1f1, #fff, #11f);
What statement creates an array in JavaScript?
[] {} new Object()
var arr = [1, "", ""];
Indicate the PHP function used to redirect to other page.
function() header() switch()
Simple arithmetic calculator in PHP

Last accessed pages

  1. Days between two dates, or of a specified week, in PHP MySQL (3365)
  2. Working with HTML attributes in PHP (12405)
  3. Moving html element to a random direction (3839)
  4. Output or Force Download MP3 with PHP (4939)
  5. querySelector and querySelectorAll (27243)

Popular pages this month

  1. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (72)
  2. PHP Unzipper - Extract Zip, Rar Archives (64)
  3. SHA256 Encrypt hash in JavaScript (40)
  4. Read Excel file data in PHP - PhpExcelReader (32)
  5. SHA1 Encrypt data in JavaScript (29)