Hướng dẫn w3schools html calculator

❮ HTML tag

Example

Perform a calculation and show the result in an element:


 
  +
  =

Try it Yourself »


Definition and Usage

The for attribute specifies the relationship between the result of the calculation, and the elements used in the calculation.


Browser Support

The numbers in the table specify the first browser version that fully supports the attribute.

Attribute
for 10.0 13.0 4.0 7.0  11.5

Syntax

Attribute Values

ValueDescription
element_id Specifies a space separated list of ids of one or more elements that specifies the relationship between the result of the calculation, and the elements used in the calculation

❮ HTML tag


❮ CSS Functions Reference

Example

Use calc() to calculate the width of a

element:

#div1 {
  position: absolute;
  left: 50px;
  width: calc(100% - 100px);
  border: 1px solid black;
  background-color: yellow;
  padding: 5px;
  text-align: center;
}

Try it Yourself »


Definition and Usage

The calc() function performs a calculation to be used as the property value.

Version:CSS3

Browser Support

The numbers in the table specify the first browser version that fully supports the function.

Numbers followed by -webkit-, -moz-, or -o- specify the first version that worked with a prefix.

Function
calc() 26.0
19.0 -webkit-
9.0 16.0
4.0 -moz-
7.0
6.0 -webkit-
15.0


CSS Syntax

ValueDescription
expression Required. A mathematical expression. The result will be used as the value.
The following operators can be used: + - * /

❮ CSS Functions Reference


PHP program to create a Simple Calculator:

The below program is to create a Simple Calculator in PHP.
Example

DOCTYPE html>
<html>
<body>
 
php
ini_set('display_errors',0);
 
if( isset( $_REQUEST['calculate'] ))
{
$operator=$_REQUEST['operator'];
$n1 = $_REQUEST['first_value'];
$n2 = $_REQUEST['second_value'];
 
if($operator=="+")
{
$res= $n1+$n2;
}
if($operator=="-")
{
$res= $n1-$n2;
}
if($operator=="*")
{
$res =$n1*$n2;
}
if($operator=="/")
{
$res= $n1/$n2;
}
 
if($_REQUEST['first_value']==NULL || $_REQUEST['second_value']==NULL)
{
echo "";
}
}
?>
 
<form>
<table style="border:groove #00FF99">
 
<tr>
<td style="background-color:turquoise; color:black; font-family:'Times New Roman'">Enter Numbertd>
<td colspan="1">
<input name="first_value" type="text" style="color:red"/>td>
tr>
 
<tr>
<td style="color:red; font-family:'Times New Roman'">Select Operatortd>
<td>
<select name="operator" style="width: 63px">
<option>+option>
<option>-option>
<option>*option>
<option>/option>
select>td>
tr>
 
<tr>
<td style="background-color:turquoise; color:black; font-family:'Times New Roman'">Enter Numbertd>
<td class="auto-style5">
<input name="second_value" type="text"  style="color:red"/>td> 
tr>
 
<tr>
<td>td>
<td><input type="submit" name="calculate" value="Calculate" style="color:wheat;background-color:rosybrown" />td>	 
tr>
 
<tr>
<td style="background-color:turquoise;color:black">Output = td>
<td style="color:darkblue">php echo $res;?>td>
tr>	
 
table>
form>
 
body>
html>