PHP Basic Tutorial

Math Functions

PHP provides nearly fifty functions that can perform various mathematical functions. Combined with mathematical operators, they can make your homework a lot easier!

The simplest math functions are those that only require a single parameter. This parameter is usually a number, which is accepted either as a variable or a string. The ceil() function, for example, will return a number rounded up to the nearest full number.

<?php
$num = 55;
echo ceil($num);
// Result Will Be: 55

$result = ceil(295.34);
echo $result;
// Result Will Be: 296
?>

As you can see, the results of these functions can be echoed, stored in a variable, etc.

Some math functions accept multiple parameters. The rand() function, for example, generates a random number in between the two numbers that it is given. Multiple parameter are separated by commas.

<?php
echo rand(0, 500);
// Result Is: 488
?>

If you refresh the page, you will see the above result change each time to a random number between 0 and 500.

Below is a list of almost twenty useful math functions, along with a brief description and required parameters.