Mathematics Arithmetic
In this tutorial we will be looking in detail into how using the inbuilt functions of PHP we can run the oldest branch of mathematics, Arithmetic.
PHP lets you run simple maths easily, using the in built functions with PHP we are able to do add, subtract, multiply and divide. PHP does also include extra mathematic functions which can be found
http://php.net/manual/en/ref.math.php we will go into more of these functions in later tutorials.
For now lets start by running some simple add and subtract formulas. In our first example we will add the numbers 6 and 10.
First we will create an array in which we can store the numbers we want to add up.
[code='php']$numbers = array("6", "10");[/code}
We have assigned the array to a variable $numbers, this is so we are able to call upon the array further into the script.
Now we need to run the question to add up both numbers.
[code='php']$answer = $numbers["0"] + $numbers["1"];[/code}
Using PHP simple maths functions are as simple as that to run you put the number you need then the symbol and finally the other number. You then assign this to a variable and that will output your answer. It’s that simple.
[code='php']echo $numbers["0"] . " + " . $numbers["1"] . " = " . $answer;[/code}
To finish this off we have echoed the question and the answer. For subtraction you can use the same code as above but change the second line $answer to the following.
[code='php']$answer = $numbers["1"] - $numbers["0"];[/code}
Now we have subtraction you should be getting the format of this
Multiplication
[code='php']$answer = $numbers["1"] * $numbers["0"];[/code}
Division
[code='php']$answer = $numbers["1"] / $numbers["0"];[/code}
So now we have mastered the basics, but all we have been doing is adding, subtracting, multiplying and dividing 2 numbers. What if we require doing more than just the 2, well with the array we created previously we are able to add as many numbers as we want for example.
[code='php']$numbers = array("6", "10", "15", "32", "99", "1", "27");[/code}
As we have stored these numbers in an array we will save time by instead of writing out each number we will loop through the array, in this instance we will be adding up the numbers.
[code='php']$question = "0";
foreach($numbers as $value){
$question = $question + $value;
}[/code}
As you may have noticed before running through the foreach we have set $question to 0. This may look stupid but depending on the level of reporting your system is set to leaving this off may produce an error as the first time it loops you would be adding a value to in its case an unset variable.
Once we have set $question using the foreach we loop through the $numbers array assigning each number to $value as it loops, inside the loop we retrieve $question and add the value to it. This is then set to $question so it can continuously add to it.
[code='php']echo "Total sum is: " . $question;[/code}
We have now just echoed the answer to the question. So now you should have something similar to this.
[code='php']
$numbers = array("6", "10", "15", "32", "99", "1", "27");
$question = "0";
foreach($numbers as $value){
$question = $question + $value;
}
echo "Total sum is: " . $question;
?>[/code}
Using the snippet used earlier in this tutorial, this code can be modified to subtract, multiply or divide as well.
I hope my tutorial has provided you with a better insight into using PHP to run arithmetic, and if you need someplace to start messing around with PHP. I now provide web hosting simply visit my website for further details.
Leave a Reply
You must be logged in to post a comment.





