PHP is open source scripting language. It\'s widely used to develop web applications.  Home Web Programming PHP Php calculator

Php calculator


PHP CalculatorThis 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



Author's URL: Cable
PHP is open source scripting language. It\'s widely used to develop web applications. More PHP Tutorials: Featured Materials | Fresh Materials | More PHP Tutorials at LearnPHP.org

Reader's comments
comments aaaaahhhhhh September 22, 2011 says:
f*ck you.. theres no display
Reply
comments NOYB September 20, 2011 says:
How about coming up with an original code that takes a single-line of input{Eg :- (((25+6)/5)*45)-15+79}, without using pre-built php evaluation functions like "eval"?
Reply
comments bulawi September 08, 2011 says:
unable to run using the ff codes..
Reply
comments ok September 04, 2011 says:
<?
$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
comments never mind its good code September 04, 2011 says:
<?
$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
comments andrewskyworker August 21, 2011 says:
<?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)
{
$result=$tal1* $tal2;
echo"två tal av

Reply
comments andrewskyworker August 21, 2011 says:
hi i am a beginner so i hope this code will help beginner too ,it using class ,function, switch to create caculator
<?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
comments andrewskyworker August 21, 2011 says:
hi i am a beginner so i hope this code will help beginner too ,it using class ,function, switch to create caculator
<?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
comments andrewskyworker August 21, 2011 says:
hi i am a beginner so i hope this code will help beginner too ,it using class ,function, switch to create caculator
<?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
comments andrewskyworker August 21, 2011 says:
hi i am a beginner so i hope this code will help beginner too ,it using class ,function, switch to create caculator
<?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
comments andrewskyworker August 21, 2011 says:
hi i am a beginner so i hope this code will help beginner too ,it using class ,function, switch to create caculator
<?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
comments andrewskyworker August 21, 2011 says:
here is my code to support to new beginner as me
<?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
comments Camille Anne Obusan July 15, 2011 says:
WHO KNOWS THE CODE OF CALCULATOR USING PHP CODE?
Reply
comments Camille Anne Obusan July 15, 2011 says:
Hello :) GO TO HE;;
Reply
comments ew June 27, 2011 says:
wat is the out put of this
Reply
comments ali March 30, 2011 says:
fine ain't no answer.
Reply
comments kannan February 18, 2011 says:
ok fine....
Reply
comments balo khan January 28, 2011 says:
thanx alot ,, i really need it
Reply
comments christain October 05, 2010 says:
kilang ba cya mamatay?
Reply
comments Qaisir September 22, 2010 says:
<input type="text" name="numa" size="10">
<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
comments srishty February 28, 2011 says:
sir where u put the Onclick event...withot that we will not be able to get the result by merely clicking on the button
Reply
comments Qaisir September 22, 2010 says:
It shld be like this ..

----
<?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
comments priyan November 11, 2011 says:
Nice code... helpful for me...
Reply
comments asad July 31, 2010 says:
very poor..................
Reply
comments sujeet June 28, 2010 says:
this is nic code
Reply
comments keshav August 23, 2011 says:
just Execute then comment
Reply
Add comments to "Php calculator"

Captcha