# Math Function In PHP

In this article, we will look at PHP math functions and demonstrate how they can be used to solve problems.

The PHP language has a set of math functions that let you perform mathematical operations on numbers in a simple, easy-to-use way. ## PHP Math

PHP Math refers to the ability to perform arithmetic operations such as addition, subtraction, multiplication, and division. In addition, it can perform more complex mathematical functions such as calculating the square root, logarithm, sine, cosine, and tangent of a number.

Depending on your needs, you can use these functions for basic arithmetic or more advanced calculations.

Following are some of the PHP math functions:

 Math Functions Overview pi() Returns the absolute value of a number. min() Rounds a number to the nearest integer. max() Rounds a number up to the nearest integer. abs() Rounds a number down to the nearest integer. sqrt() Calculates the square root of a number. round() Raises a number to a given power.

## PHP pi() Function

The pi() function is a built-in PHP math function that returns the value of the mathematical constant pi ( π ).

The value of Pi is an irrational number equal to the length of 3.1415926535898, approximately.

There are no arguments required for the pi() function and it is very easy to use. Here’s an example:

#### Example:

<?php echo(pi()); // returns 3.1415926535898 ?>

Below is an example for finding area of circle using pi( π ) function:

#### Example:

<?php // for area of circle \$r = 3; //radius \$area_of_circle = (pi()*(\$r*\$r)); //formula of area of circle echo(\$area_of_circle); // returns 28.274333882308 ?>

### Example Explanation:

1. First we have defined a variable \$r and assigns it a value of 3, which represents the radius of the circle.
2. Then we calculate the area of the circle using the formula (pi()*(\$r* \$r)) where pi() is a built-in PHP math function that returns the mathematical constant π and \$r * \$r calculates the square of the radius.
3. The result of this calculation is stored in a variable called \$area_of_circle.
4. Finally, we have used the echo statement to display the value of \$area_of_circle on the screen.

The output of the code is the result of the area calculation, which is approximately 28.274333882308.

## PHP min() and max() Functions

A min() and max() function is another mathematics function that is useful in finding the minimum and maximum values of an array of numbers or a set of numbers in a list.

When working with numerical data in PHP, these functions are very helpful and will save you time and effort.

Following are examples of using the min() and max() functions:

#### Example:

<?php echo(min(2, 78, 32, 42, -100, -3)); // returns -100 echo(max(2, 78, 32, 42, -100, -3)); // returns 78 ?>

This example defines an array \$a with 5 elements, including numbers of different types and a string:

#### Example:

<?php \$a = array(9.2, 8, 5, "2", 6); echo("Min value: ". min(\$a) . "<br>"); // returns 2 echo("Max value: ". max(\$a)); // returns 9.2 ?>

A default argument value is a value assigned to a function parameter that is used when the argument is not explicitly passed to the function.

This allows a function to be called with fewer arguments than it requires, and provides a default value for any missing arguments.

The min(\$a) function returns the smallest value in the array. Since “2” is a string, it is automatically converted to a number before the minimum value is calculated, resulting in 2.

Max(\$a) returns the largest value in the array as a number. As output, we get 9.2, which is a float.

## PHP Math abs() Function

Abs() returns the absolute value of a number. In mathematical terms, the absolute value of a number is its distance from zero, no matter what the number is, whether it is positive or negative.

As an example, the absolute value of -6 is 6, and the absolute value of 6 is also 6:

#### Example:

<?php echo(abs(-6)); // returns 6 ?>

In the following example, if you pass a string parameter to abs(), it returns 0:

#### Example:

<?php \$num="Hello"; echo "String: " . \$num . ""; echo "absolute value : " . abs(\$num) . ""; ?>

## PHP Math sqrt() Function

sqrt() function gives the square root of a number. The square root is the number multiplied by itself that equals the original number.

For example, the square root of 9 is 3 because 3 times 3 equals 9:

#### Example:

<?php echo(sqrt(9)); // returns 3 ?>

In another example -9 is not defined, so it returns NaN:

#### Example:

<?php echo(sqrt(-9)); // returns NaN ?>

## PHP Math round() Function

Rounding floats in PHP is achieved by round().

It takes two arguments: the number to be rounded and another optional argument specifying how many decimals to round.

### Syntax

`round(\$number, \$precision)`

### Syntax Explanation:

• \$number : This is required. Round the number.
• \$precision : This is optional. To round how many decimal places. By default, it’s 0.

In the following example, the round() function rounds the value to floor and ceiling values (example: ceiling is a low value & floor is a large value):

#### Example:

<?php echo(round(0.51)); // returns 1 echo(round(0.48));// returns 0 ?>

We can also set the decimal points as shown in the below example:

#### Example:

<?php echo(round(2.37454,3)); // returns 2.374 echo(round(2.68124, 2));// returns 2.68 ?>

## PHP Math Rand() Function

In order to generate a random integer, we can use the rand() function.

Below is an example showing that rand() gives a random number:

#### Example:

<?php echo(rand()); ?>

In the following code example, you can see that every time we call the rand() function we get a different random value:

#### Example:

<?php \$a = rand(); echo \$a . "<br>"; echo rand()."<br>"; print rand(); ?>

There are optional parameters \$max and \$min that you can use to specify the lowest integer and the highest integer that will be returned for the random number in order to gain more control over the random number.

As an example, if you want a random integer between 1 and 10 (inclusive), then you would use rand(1, 10):

#### Example:

<?php echo(rand(1, 10)); ?>

Another example shows a random number between 50 and 100:

#### Example:

<?php echo(rand(50, 100)); ?>
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0
{"ft_lang_label":"__label__en","ft_lang_prob":0.7752561,"math_prob":0.9939674,"size":5113,"snap":"2023-40-2023-50","text_gpt3_token_len":1138,"char_repetition_ratio":0.1606968,"word_repetition_ratio":0.021615472,"special_character_ratio":0.23274007,"punctuation_ratio":0.116,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9997286,"pos_list":[0,1,2,3,4],"im_url_duplicate_count":[null,1,null,null,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2023-12-06T08:45:09Z\",\"WARC-Record-ID\":\"<urn:uuid:8575882d-b475-46e5-97d2-4bfdd8e70782>\",\"Content-Length\":\"285516\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:8c201cf1-4142-4db3-926b-103e05a9180d>\",\"WARC-Concurrent-To\":\"<urn:uuid:045cd4f8-8d2a-436a-887f-448446e0918c>\",\"WARC-IP-Address\":\"104.21.27.131\",\"WARC-Target-URI\":\"https://mrexamples.com/php/php-math/\",\"WARC-Payload-Digest\":\"sha1:KPPNVPL3FNEVTC7BS2YQDNALU5M4HKUS\",\"WARC-Block-Digest\":\"sha1:XCKGXXTHHEO2LSFILG7J5OGDVBQ473ZH\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2023/CC-MAIN-2023-50/CC-MAIN-2023-50_segments_1700679100583.31_warc_CC-MAIN-20231206063543-20231206093543-00813.warc.gz\"}"}