Updated March 15, 2023
Overview of PHP Math Functions
In this article, we are going to discuss PHP math functions. PHP stands for Hypertext Preprocessor. PHP is a programming language that could be used to build small applications like simple form etc to large scale enterprise applications. It is a server-side-based scripting language. Every programming language has lots of in-built default functionalities.
These functionalities help the developer to quickly write the required code. These inbuilt functionalities contain logics within which is required as per our requirement. These inbuilt functionalities may be on string-based functionalities, array-based functionalities, number-based functionalities, JSON based functionalities, etc
Now let us look at what is PHP Math Functions
PHP Math Functions
They are inbuilt functionalities of PHP as a programming language. The basic role of these functions is to provide a mechanism in which a developer can do some sort of math calculations or similar stuff. These provide a quick hands-on for development without writing a long piece of code. Now with that let us know the range of these PHP math functions
Range of PHP math functions
The range of these php math functions is within integer and float types. Range of integer data type in PHP for a 32-bit computer is -2,147,483,647 to 2,147,483,647. Any number smaller than -2,147,483,647 or any number greater than 2,147,483,647 or any number smaller than -2,147,483,647 is considered as float.
Now we will try to understand different PHP math functions, along with its use an Example:
1. abs() function
It was introduced in PHP 4+ version. It returns the absolute value of the number. The return type of the function is float or integer number depending upon what type of argument passed in the function.
Example:
<!DOCTYPE html>
<html>
<body>
<?php
echo(abs(3.5) . "<br>");
echo(abs(-3.5) . "<br>");
echo(abs(5) . "<br>");
echo(abs(-5));
?>
</body>
</html>
Output:
2. acos() function
It was introduced in PHP 4+ version. It expects argument in the range of -1 to +1. If number out of specified range is passed in an argument then it returns NaN otherwise it returns arc cosine value of the number. The return type of the function is arc cosine of a number
Example:
<!DOCTYPE html>
<html>
<body>
<?php
echo(acos(0.35) . "<br>");
echo(acos(-0.35) . "<br>");
echo(acos(5) . "<br>");
echo(acos(0.7253));
?>
</body>
</html>
Output:
3. asin() function
It was introduced in PHP 4+ version. It expects argument in the range of -1 to +1. If number out of specified range is passed in an argument then it returns NaN otherwise it returns arc sine value of the number. The return type of the function is arc sine of a number
Example:
<!DOCTYPE html>
<html>
<body>
<?php
echo(asin(0.35) . "<br>");
echo(asin(-0.35) . "<br>");
echo(asin(5) . "<br>");
echo(asin(0.7253));
?>
</body>
</html>
Output:
4. ceil() function
It was introduced in PHP 4+ version. It rounds a number up to the nearest integer. For example, the ceil of 3.2 will give 4. It returns the whole number in the form of nearest integer which is greater than the passed argument
Example:
<!DOCTYPE html>
<html>
<body>
<?php
echo(ceil(3.35) . "<br>");
echo(ceil(-4.35) . "<br>");
echo(ceil(5) . "<br>");
echo(ceil(14.8114700666069059));
?>
</body>
</html>
Output:
5. floor() function
It was introduced in PHP 4+ version. It rounds a number down to the nearest integer. For example, the floor of 3.2 will give 3. It returns the whole number in the form of nearest integer which is smaller than the passed argument
Example:
<!DOCTYPE html>
<html>
<body>
<?php
echo(floor(3.35) . "<br>");
echo(floor(-2.35) . "<br>");
echo(floor(5) . "<br>");
echo(floor(14.811470062));
?>
</body>
</html>
Output:
6. pi() function
It was introduced in PHP 4+ version. It returns value of a PI and its return type is a float.
Example:
<!DOCTYPE html>
<html>
<body>
<?php
echo(pi() . "<br>");
?>
</body>
</html>
Output:
7. pow() function
It was introduced in PHP 4+ version. It accepts two arguments say x and y. It calculates x raised to the power of y. its return type is integer or float which depends upon the nature of the argument
Example:
<!DOCTYPE html>
<html>
<body>
<?php
echo(pow(2,3) . "<br>");
echo(pow(2,4) . "<br>");
echo(pow(5,6) . "<br>");
echo(pow(3,5));
?>
</body>
</html>
Output:
8. log() function
It was introduced in PHP 4+ version. It accepts two arguments say x and y where x is a number and y is the logarithm of a number to base. If y is not passed then the default value ‘e’ is assumed. Its return type is float
Example:
<!DOCTYPE html>
<html>
<body>
<?php
echo(log(2.718) . "<br>");
echo(log(2) . "<br>");
echo(log(1) . "<br>");
echo(log(0));
?>
</body>
</html>
Output:
9. log10() function
It was introduced in PHP 4+ version. It accepts one argument says x where x is a number whose base 10 logarithm needs to be calculated. Its return type is float
Example:
<!DOCTYPE html>
<html>
<body>
<?php
echo(log10(656) . "<br>");
echo(log10(455) . "<br>");
echo(log10(145) . "<br>");
?>
</body>
</html>
Output:
10. round() function
It was introduced in PHP 4+ version. It rounds a number. It expects three parameters where the first parameter is number, the second parameter is for precision and the third argument is for mode. The only first argument is mandatory
Example:
<!DOCTYPE html>
<html>
<body>
<?php
echo(round(3.35) . "<br>");
echo(round(-2.35) . "<br>");
echo(round(5) . "<br>");
?>
</body>
</html>
Output:
Apart from specified PHP math functions, there are several other math functions that could be used as per our requirements. We could run above described above-mentioned PHP code snippet to execute it
Let us know how to run an example code snippet.
- Install php5 or 5+
- Install localhost say WampServer etc
- Now create a file and name it as index.php
- Paste example snippet in the file created
- Run localhost and run index.php on browser
Conclusion
Php is a very vast programming language, we can learn it in order to make web applications. It is used to handle server-side scripting logic. Although we could insert our HTML code also within PHP as we have used in example snippets.
Php has a large database for inbuilt functionalities. There are several functionalities that use string as a parameter, other functionalities use an array. These inbuilt functionalities help us to solve our requirements without writing much code.
Recommended Articles
This has been a guide to PHP Math Functions. Here we discuss the introduction, Range of the functions, and detailed explanation of the different functions along with examples in PHP. You can also go through our other suggested articles to learn more –