**Using PHP Mathematical Functions**

*Tutorial Level*: Beginner

One of the great aspects of PHP programming is that the language includes numerous functions for mathematic equations and problem solving. A complete list of these mathematic functions can be found in the PHP Manual, but explaining every function would be impractical and would only bore those trying to learn PHP. Instead, we going to see some of the most common mathematic equations and functions, such as adding, subtracting, multiplying, dividing, greater-than or less-than, rounding, absolute value and others. While all these may seem very simple, many times PHP involves a great deal of numbers, and these numbers can grow and become large equations, for example: counting the number of seconds since a date and time, and knowing how to minipulate these numbers can be a great benifit to any programmer.

**Simple Math**

First, let’s start out with the basics: addition, subtraction, multiplication and division. These are the basic functions that will make up larger equations and the ones we will build on as we get into more advanced math. For examples, we will create a variable for the equation, and then print it through the *echo* function. Let’s start with addition:

<?

$add = 1 + 1;

echo(“$add”);

?>

This example would print the value *2* on the webpage when added to a PHP script. Next, we can try the subtraction function. This function works the same as the addition function, but uses a minus (-) rather than a plus (+), just as in normal grammar school math. For example:

<?

$sub = (5 – 2);

echo(“$sub”);

?>

This equation would produce a value of *3* on the server. Now that we know how to do the simple stuff, we can create more complex equations by using parantheses in the equation. This equation combines the use of addition and multiplication, along with the use of parantheses to follow order of operations:

<?

$par = (11 + 4) * 4;

echo(“$par”);

?>

With the use of multiplication (*), this equation would produce a value of *60* because *11 + 4 = 15*, then *15 * 4 = 60*. Rather than breaking this into a two step process, the same way the problem can be explained, using parentheses can save space and make this a one step process. Just be careful how many levels of brackets you use in an equation, because it can become cluttered and confusing. For more complex equations, it is okay to only use a small amount of brackets to better see the problem. The last function is division. For example:

<?

$div = 44 / 11;

echo(“$div”);

?>

The division tag (/) is the same as that on a scientific caluculator. Remember when doing PHP math that the signs are scientific, and not simple (IE: multiplication sign is not *x*, but ***). With this problem above, we would get the value *4*, because *44 divided by 11 = 4*. While it may seem like learning 2nd grade math all over again, it is important to understand the basics before moving on.

**Greater or Less-Than**

While it may not seem to be a big part in programming, the greater-than and less-than functions are very useful. Most of the time, these functions are used in conjunction with the *IF* or *ELSE* statements. For example, if a number is greater-than or equal to 0, than it is positive, else, if the number is less-than or equal to -1, than the number is negative, and will need to be multiplied by (-1) to become positive. As well as using the greater-than ( > ) and less-than ( < ) signs, the function for greater-than or equal (>=) or less-than or equal (<=) can also be use. The following is the PHP version of the example stated in this paragraph…

<?

if($num >= 0)

{

echo(“$num is positive”);

}

elseif($num <= -1)

{

$pos = $num * -1;

echo(“$pos is positive because $num (times) -1 (equals) a positive number”);

}

?>

If a number is plugged into the equation (*$num* is given a value as a number), than the script will see if the number is positive or negative, and if it is negative, than it will be multiplied by (-1) to become positive. This is the same prinicple behind absolute value, which we will explore further in the tutorial. As you can see in the example above, the simple math functions are essential to equations in PHP.

**Rounding Numbers**

Rounding is another important part of PHP programming, and can play a big part in Content Management Systems and pagination, which is a script that divides long sets of information into separate, and even pages. There are three types of rounding we will work with: standard rounding, rounding up and rounding down. All these methods are used with a decimal and round to an integer. Standard rounding is a method of rounding used in most math, where if a number is greater-than or equal to 5, than the number is rounded up, but if the number is less than 5, the number is rounded down. The function is called *round()* for this method. The following is an example of this function…

<?

$num = 6.5;

$num2 = 7.3;

$round = round($num);

$round2 = round($num2);

echo(“Rounded number 1: $round | Rounded number 2: $round2”);

?>

This example would output *7* for both *$round1* and *$round2* because *6.5* would round up, and *7.3* would round down. You can also change the number of decimal points to round out to. To do this, simply add a comma and a number after the number being rounded. For example…

<?

$num = 6.3567;

$round = round($num, 3);

echo(“Rounded number: $round”);

?>

Notice the *3* after the *$num* variable in the *round* function. This means that the number will be rounded out to 3 decimal places (thousandths place). In this case, the number that would be produced is *6.357*. The last *7* would round up, and then the rounding would stop because it is set to go to the 3rd decimal place. Now that we have a grasp on the standard rounding method, let’s move onto the rounding up method. The function for rounding up is the the *ceil()*. Here is an example of this function…

<?

$num = 6.2;

$round = ceil($num);

echo(“Rounded number: $round”);

?>

This example would output *7* because *6.3* would be rounded up. The last method of rounding is the rounding down function. This function is called *floor()*. Here is an example of this function…

<?

$num = 4.9999;

$round = floor($num);

echo(“Rounded number: $round”);

?>

This example would produce a value of *4* because the number *4.9999* would be forced to round down, and thus become *4*. While it may seem like an unimportant math function, rounding can be very useful when an interger is needed after a problem such as division. As stated before, pagination uses this because pages must in integers, and it would be incorrect to have a page number of *4.956*, rather than *5* for example.

**Absolute Value**

This math function is also another useful PHP function, and can also be used in things such pagination. Because it would be incorrect to have a negative page number (IE: -2, -1, 0, 1, 2, 3 rather than 1, 2, 3, 4…), removing negative numbers for the equation would make the end result be positive. It is actually very simple to obtain the obsolute value of a number. The function is called *abs()*. The following is an example of this function…

<?

$num = -3;

$abs = abs($num);

echo(“The absolute value of $num is $abs”);

?>

This would produce the value *3*, rather than *-3*. Although this tutorial only covers a few of the many mathematic functions, there are many more functions that can be used to minipulate numbers and finding the value of equations. The PHP Manual Mathematics Page is a useful resource. I hope this makes the task of programming in PHP easier for you and more fun.