This tutorial will teach you how to make a calculator using PHP.
Firstly, create a new .php document and copy/paste the following code into it.
|
<?php
/* Calculator */ if($submit) { if($operator == '*') { echo $numa * $numb; } elseif($operator == '/') { echo $numa / $numb; } elseif($operator == '+') { echo $numa + $numb; } elseif($operator == '-') { echo $numa - $numb; } } else { ?> <form method="POST" action="<?php $_SERVER['PHP_SELF']; ?>"> <input type="text" name="numa" size="10"> <input type="text" name="operator" size="2"> <input type="text" name="numb" size="10"> <input type="submit" value="Calculate" name="submit"> </form> <?php } ?> |
Legend:
* Multiply
- Subtract
/ Divide
+ Add


Reply
Reply
Reply
$num1 = $_POST['num1'] ;
$num2 = $_POST['num2'];
$operator =$_POST['operation'] ;
if($submit)
{
if($operator == '*')
{
echo $num1 * $num2;
} elseif($operator == '/')
{
echo $num1 / $num2;
} elseif($operator == '+')
{
echo $num1 + $num2;
} elseif($operator == '-')
{
echo $num1 - $num2;
}
} else { ?>
<form action='<?php $_SERVER['PHP_SELF']; ?>' method='post'>
<input name='num1' type='text'>
<select name="operation">
<option>+</option><option>-</option><op
Reply
$num1 = $_POST['num1'] ;
$num2 = $_POST['num2'];
$operator =$_POST['operation'] ;
if($submit)
{
if($operator == '*')
{
echo $num1 * $num2;
} elseif($operator == '/')
{
echo $num1 / $num2;
} elseif($operator == '+')
{
echo $num1 + $num2;
} elseif($operator == '-')
{
echo $num1 - $num2;
}
} else { ?>
<form action='<?php $_SERVER['PHP_SELF']; ?>' method='post'>
<input name='num1' type='text'>
<select name="operation">
<option>+</option>
<option>-</option>
<
Reply
//defing the calc class
$calc=new calc(); /* here create class $calc */
class calc
{
public $tal1,$tal2; /* tal mean number */
function add($tal1,$tal2)
{
$result=$tal1+$tal2;
echo "Två tal av sum $tal1 och $tal2 är $result </br>";
echo "$tal1+$tal2=$result ";
exit;
}
function minus($tal1,$tal2)
{
$result=$tal1-$tal2;
echo "två tal av minus $tal1 och $tal2 är $result </br>";
echo"$tal1-$tal2=$result";
exit;
}
function multiple($tal1,$tal2)
{
$result=$tal1* $tal2;
echo"två tal av
Reply
<?php
//defing the calc class
$calc=new calc(); /* here create class $calc */
class calc
{
public $tal1,$tal2; /* tal mean number */
function add($tal1,$tal2)
{
$result=$tal1+$tal2;
echo "Två tal av sum $tal1 och $tal2 är $result </br>";
echo "$tal1+$tal2=$result ";
exit;
}
function minus($tal1,$tal2)
{
$result=$tal1-$tal2;
echo "två tal av minus $tal1 och $tal2 är $result </br>";
echo"$tal1-$tal2=
Reply
<?php
//defing the calc class
$calc=new calc(); /* here create class $calc */
class calc
{
public $tal1,$tal2; /* tal mean number */
function add($tal1,$tal2)
{
$result=$tal1+$tal2;
echo "Två tal av sum $tal1 och $tal2 är $result </br>";
echo "$tal1+$tal2=$result ";
exit;
}
function minus($tal1,$tal2)
{
$result=$tal1-$tal2;
echo "två tal av minus $tal1 och $tal2 är $result </br>";
echo"$tal1-$tal2=
Reply
<?php
//defing the calc class
$calc=new calc(); /* here create class $calc */
class calc
{
public $tal1,$tal2; /* tal mean number */
function add($tal1,$tal2)
{
$result=$tal1+$tal2;
echo "Två tal av sum $tal1 och $tal2 är $result </br>";
echo "$tal1+$tal2=$result ";
exit;
}
function minus($tal1,$tal2)
{
$result=$tal1-$tal2;
echo "två tal av minus $tal1 och $tal2 är $result </br>";
echo"$tal1-$tal2=
Reply
<?php
//defing the calc class
$calc=new calc(); /* here create class $calc */
class calc
{
public $tal1,$tal2; /* tal mean number */
function add($tal1,$tal2)
{
$result=$tal1+$tal2;
echo "Två tal av sum $tal1 och $tal2 är $result </br>";
echo "$tal1+$tal2=$result ";
exit;
}
function minus($tal1,$tal2)
{
$result=$tal1-$tal2;
echo "två tal av minus $tal1 och $tal2 är $result </br>";
echo"$tal1-$tal2=
Reply
<?php
//defing the calc class
$calc=new calc(); /* here create class $calc */
class calc
{
public $tal1,$tal2; /* tal mean number */
function add($tal1,$tal2)
{
$result=$tal1+$tal2;
echo "Två tal av sum $tal1 och $tal2 är $result </br>";
echo "$tal1+$tal2=$result ";
exit;
}
function minus($tal1,$tal2)
{
$result=$tal1-$tal2;
echo "två tal av minus $tal1 och $tal2 är $result </br>";
echo"$tal1-$tal2=
Reply
<?php
//defing the calc class
$calc=new calc(); /* here create class $calc */
class calc
{
public $tal1,$tal2; /* tal mean number */
function add($tal1,$tal2)
{
$result=$tal1+$tal2;
echo "Två tal av sum $tal1 och $tal2 är $result </br>";
echo "$tal1+$tal2=$result ";
exit;
}
function minus($tal1,$tal2)
{
$result=$tal1-$tal2;
echo "två tal av minus $tal1 och $tal2 är $result </br>";
echo"$tal1-$tal2=$result";
exit;
}
function multiple($tal1,$tal2)
{
Reply
Reply
Reply
Reply
Reply
Reply
Reply
Reply
<select name="operator">
<option>+</option>
<option>-</option>
<option>*</option>
<option>/</option>
</select>
<input type="text" name="numb" size="10">
<input type="submit" value="Calculate" name="submit">
</form>
<?php } ?>
Reply
Reply
----
<?php
/* Calculator */
if (isset($_POST['submit']))
{
if($_POST['operator'] == '*')
{
echo $_POST['numa'] * $_POST['numb'];
} elseif($_POST['operator'] == '/')
{
echo $_POST['numa'] / $_POST['numb'];
} elseif($_POST['operator'] == '+')
{
echo $_POST['numa'] + $_POST['numb'];
} elseif($_POST['operator'] == '-')
{
echo $_POST['numa'] - $_POST['numb'];
}
} else { ?>
<form method="POST" action="<?php $_SERVER['PHP_SELF']; ?>">
<input type="text&qu
Reply
Reply
Reply
Reply
Reply