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. Output or Force Download MP3 with PHP (3103)
  2. Register and show online users and visitors (34832)
  3. Add, Change, and Remove Attributes with jQuery (40296)
  4. Get Attribute (ID, Class, Name, Title, Src) with jQuery (67904)
  5. Add text between two DIV tags (1805)

Popular pages this month

  1. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (56)
  2. JavaScript Course - Free lessons (42)
  3. Read Excel file data in PHP - PhpExcelReader (38)
  4. HTML Course - Free Lessons (37)
  5. PHP-MySQL free course, online tutorials PHP MySQL code (35)