2006-12-17 16:50:45
This tutorial will teach you how to make a basic php calculator using 1 simple class. I will work out any question with 1 operator, such as "5*4" or "89/3". To see a live demo, click here.
Before starting you will need a server with php support and some prior knowledge of classes, lets get started.
First of all here's the code:
class calculator{
var $sum;
function add($sum){
$parts = explode("+",$sum);
$answer=$parts[0]+$parts[1]; // do sum
return $answer;
}
function minus($sum){
$parts = explode("-",$sum);
$answer=$parts[0]-$parts[1]; // do sum
return $answer;
}
function multiply($sum){
$parts = explode("*",$sum);
$answer=$parts[0]*$parts[1]; // do sum
return $answer;
}
function divide($sum){
$parts = explode("/",$sum);
$answer=$parts[0]/$parts[1]; // do sum
return $answer;
}
function answer($sum){
if(strstr($sum,"+")){//plus
return $this->add($sum);
}elseif(strstr($sum,"-")){//minus
return $this->minus($sum);
}elseif(strstr($sum,"*")){//multiply
return $this->multiply($sum);
}elseif(strstr($sum,"/")){//divide
return $this->divide($sum);
}
}
}
if($_POST['submit']){
$calc = new calculator($_POST['sum']);
echo "{$_POST['sum']}=";
echo $calc->answer($_POST['sum']);
}
?>
<form method="post" name="form1"> <input type="text" name="sum" />
<input type="submit" value="Submit" id="submit" name="submit" />
</form>
If you didn't understand any of that don't worry, all will be explained...
First of all, we created our class which handles all the operations, we started our class declaration with the following code:
class calculator{
var $sum;
Which basically just says that we want to create a new class called calculator, which will require the variable $sum.
Now we create the functions that will go inside our class to handle all the main operations (+,-,/,*), each function is named after it's operator so we have the add, minus, divide and multiply functions, each function works in the same way so I will just explain how the add function works and you will be able to understand how the other's work.
First of all we define our function and the variable we need for it to work (the sum):
function add($sum){
Now we do the complicated work, for php to be able to work out a sum, it needs the three parts to it, the first number, operator and second number, as we know what the operator is we can use a handy function called explode() to separate the sum into our 2 parts, the function explode works by splitting a string by a certain character or characters, visit the php website for more information.
$parts = explode("+",$sum);
Now we have our 2 parts of the string, which are stored in the array "$parts", and we can work out the sum, store the answer in a variable ($answer), and use the "return" operator, to give out the answer to the sum, simple.
$answer=$parts[0]+$parts[1]; // do sum
return $answer;
}
Now we just create a similar function for our three other operations, and make one last function in the class, to compile our existing methods. To do this we will create one last function called "answer", it will require one variable, $sum. This variable works out which operator is used in the sum, to call the correct function (add, minus, multiply, divide). To determine which operator can be found in the sum, we use the strstr() function, click here to look it up on php website. This function checks to see if a string can be found within another string. We simply run though a series of if's and elseif's to find out which operator is used. Once we know which operator is used, we can call the correct function, and return it to be seen.
function answer($sum){
if(strstr($sum,"+")){//plus
return $this->add($sum);
}elseif(strstr($sum,"-")){//minus
return $this->minus($sum);
}elseif(strstr($sum,"*")){//multiply
return $this->multiply($sum);
}elseif(strstr($sum,"/")){//divide
return $this->divide($sum);
}
}
Most of this will probably be quite understandable, however the line;s starting with return, you may not have come across before, "$this" refers to the current class, and "->divide($sum)" calls the function divide() from this class.
Now we've finished dealing with the class we can move on to handling the form when it has been posted.
if($_POST['submit']){
$calc = new calculator($_POST['sum']);
echo "{$_POST['sum']}=";
echo $calc->answer($_POST['sum']);
}
Here we check to see if the form has been submitted, on the first line. Then start up our calculator class ,on the second line. Then on the 3rd and 4th lines we echo out the sum and the answer for the user to see.
Now we just create a simple html form, with a textfield called sum, and a submit button called submit, you can use the html below:
<form name="form1" method="post"> <input type="text" name="sum" />
<input type="submit" name="submit" id="submit" value="Submit" />
</form>
Thanks for reading this, I hope it helped you. Check back soon for some more tutorials.