Jump to content

  •  

Photo
- - - - -

P5: Maths

PHP4Beginners

  • Please log in to reply
No replies to this topic

#1 DraxxTV

DraxxTV

    Member

  • SuperGeek
  • PipPip
  • 14 posts

Posted 06 July 2014 - 06:14 PM

Addition and Subtraction

The first thing we will be doing is learning to add two numbers together. We can do this with the section of code below:

10 + 8 = <?php echo 10+8; ?>

This will then output:

10 + 8 = 18

Now, If you would like to take two numbers away from each other you can do so with the following:

18 - 11 = <?php echo 18-11; ?>

Multiplication and Division

The next thing we will be talking about is how to multiply two numbers together. This is very similar to the posts above.

3 x 3 = <?php echo 3*3; ?>

This would output 3 x 3 = 9

This is also very similar to how division works.

100 / 3 = <?php echo 100/3; ?>

When you do this, you would see

100 / 3 = 33.333333333333

Other

As well as the basic things listed above, you can also do a range of other things in php which are related to maths.

Round

When doing working with decimals, you may find that you need to round up your answers. This would be very useful for the division above. To do this, we can use phps round function.

Here are some examples of how this can be used:

<?php
echo round(3.4); // 3
echo round(3.5); // 4
echo round(3.6); // 4
echo round(3.6, 0); // 4
echo round(1.95583, 2); // 1.96
echo round(1241757, -3); // 1242000
echo round(5.045, 2); // 5.05
echo round(5.055, 2); // 5.06
?>

An example of using this with the division we did before would be

100 / 3 = <?php echo round(100/3); ?>

This would output

100 / 3 = 33

Square Root

If you would like to find the square root of a number, you can find it with the following :

<?php echo sqrt(69); ?>

There are also plenty of other maths functions which can be found on the w3schools website here: http://wpi.cc/2dC







Also tagged with one or more of these keywords: PHP4Beginners