# Simple Calculator in PHP with basic calculator functions Hi, here I am posting code snippet of a simple calculator which performs basic functions like add, subtract, divide, multiply of two numbers and also square, square root and cube of a number. No database is used for creating this calculator. A class named “calc” is defined in this which has the above said methods and when they are called the calculation is done according to them. It is really helpful for learning purpose. This code snippet teaches us how to create a class and methods and also how to call them.

{code type=codetype}

<?php
// Defining the “calc” class
class calc {
var \$number1;
var \$number2;

{
\$result =\$number1 + \$number2;
echo “The sum of \$number1 and \$number2 is \$result<br><br>”;
echo “\$number1 + \$number2 = \$result”;
exit;
}

function subtract(\$number1,\$number2)
{
\$result =\$number1 – \$number2;
echo “The difference of \$number1 and \$number2 is \$result<br><br>”;
echo “\$number1 &#045 \$number2 = \$result”;
exit;
}

function divide(\$number1,\$number2)
{
\$result =\$number1 / \$number2;
echo “\$number1 divided by \$number2 is \$result<br><br>”;
echo “\$number1 ? \$number2 = \$result”;
exit;
}

function multiply(\$number1,\$number2)
{
\$result =\$number1 * \$number2;
echo “The product of \$number1 and \$number2 is \$result<br><br>”;
echo “\$number1 x \$number2 = \$result”;
exit;
}

function square(\$number)
{
\$result = \$number * \$number;
echo “The square of \$number is \$result<br><br>”;
echo “\$number &#94; 2 = \$result”;
}

function squareroot(\$number)
{
\$result = sqrt(\$number);
echo “The square root of \$number is \$result<br><br>”;
}

function cube(\$number)
{
\$result = \$number * \$number * \$number;
echo “The cube of \$number is \$result<br><br>”;
echo “\$number1 &#94; 3 = \$result”;
}
}
//Creating object of class
\$calc = new calc();
?>
<title>PHP Calculator </title>
<h2>Calculator 1</h2>
<form name=”calc” action=”” method=”POST”>
Number 1: <input type=”text” name=”value1″><br>
Number 2: <input type=”text” name=”value2″><br>
Operation:
<select name=”oper”>
<option value=”subtract”>Subtraction</option>
<option value=”divide”>Division</option>
<option value=”multiply”>Multiplication</option>
</select>
<br>
<input type=”submit” value=”Calculate” name=”submit”>
</form>
<br>
<br>
<h2>Calculator 2</h2>
<form name=”calc2″ action=”” method=”POST”>
Number : <input type=”text” name=”value”><br>
Operation:
<select name=”operat”>
<option value=”square”>Square</option>
<option value=”squareroot”>Square Root</option>
<option value=”Cube”>Cube</option>
</select>
<br>
<input type=”submit” value=”Calculate” name=”submit1″>
</form>
<?php
if(\$_POST[‘submit’]){
\$number1 = \$_POST[‘value1’];
\$number2 = \$_POST[‘value2’];
\$oper = \$_POST[‘oper’];
if(!\$number1){
echo “<font color=’red’>Please enter first number in calculator 1</font>”;
exit;
}
if(!\$number2){
echo “<font color=’red’>Please enter second number in calculator 1</font>”;
exit;
}
if(!\$oper){
echo “<font color=’red’>Please select operator in calculator 1</font>”;
exit;
}
if(!eregi(“[0-9]”, \$number1)){
echo “<font color=’red’>First number must be digits in calculator 1</font>”;
exit;
}
if(!eregi(“[0-9]”, \$number2)){
echo “<font color=’red’>Second number must be digit in calculator 1</font>”;
exit;
}
}
if(\$oper == “subtract”){
\$calc->subtract(\$number1,\$number2);
}
if(\$oper == “divide”){
\$calc->divide(\$number1,\$number2);
}
if(\$oper == “multiply”){
\$calc->multiply(\$number1,\$number2);
}
}
?>
<?php
if(\$_POST[‘submit1’]){
\$number = \$_POST[‘value’];
\$operat = \$_POST[‘operat’];
if(!\$number){
echo “<font color=’red’>Please enter number in calculator 2</font>”;
exit;
}
if(!\$operat){
echo “<font color=’red’>Please select operator in calculator 2</font>”;
exit;
}
if(!eregi(“[0-9]”, \$number)){
echo “<font color=’red’>Value must be digits in calculator 2</font>”;
exit;
}
if(\$operat == “square”){
\$calc->square(\$number);
}
if(\$operat == “cube”){
\$calc->cube(\$number);
}
if(\$operat == “squareroot”){
\$calc->squareroot(\$number);
}
}
?>

{/code}

## Related Posts

• […] here: Simple Calculator in PHP with basic calculator functions | My PHP … Related Posts:PHP Canada Post Shipping Rate Calculator 1.0 | Software, Games …Css/xhtml/php […]

• […] original post here: Simple Calculator in PHP with basic calculator functions | My PHP … Related Posts:DLE Referer v.7.0.1Does WordPress have the functions to track who came to my blog and […]

• […] post: Simple Calculator in PHP with basic calculator functions | My PHP … Share and […]

• […] Adres URL: Simple Calculator in PHP with basic calculator functions | My PHP … […]

• […] the original post: Simple Calculator in PHP with basic calculator functions | My PHP … ??: php ??: basic-functions, calculator, like-add, posting-code, simple-calculator, […]

• […] Simple Calculator in PHP with basic calculator functions | My PHP … […]

• […] See the article here: Simple Calculator in PHP with basic calculator functions | My PHP … […]

• […] See the rest here: Simple Calculator in PHP with basic calculator functions | My PHP … […]

• […] Simple Calculator in PHP with basic calculator functions | My PHP Scripts | A Hub for free PHP Scrip… […]

• My cousin recommended this blog and she was totally right keep up the fantastic work!

• Thanks for taking the time to make this post, it was a good read

• This is great what you are doing. Please kee up the work, I am learning a lot from your scripts.

• hello
there has some undefine problem in if(\$_post[‘sumbit’])
it should be if(isset (\$_post[‘sumbit’]))
you are doing great,
now i am understand how to use class
thank

• Great tutorial, thanks!! 🙂
Jon recently posted…Web Design London | Website Designers in London • This source codes has error at me..

echo “The sum of \$number1 and \$number2 is \$result”;

Help me what to do? Hehehe.. Newbie here.