PHP Math Functions

WBOY
Release: 2024-08-29 12:46:37
Original
572 people have browsed it

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.

ADVERTISEMENT Popular Course in this category PHP DEVELOPER - Specialization | 8 Course Series | 3 Mock Tests

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

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>
Copy after login

Output:

PHP Math Functions

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>
Copy after login

Output:

PHP Math Functions

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>
Copy after login

Output:

PHP Math Functions

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>
Copy after login

Output:

PHP Math Functions

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>
Copy after login

Output:

PHP Math Functions

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>
Copy after login

Output:

PHP Math Functions

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>
Copy after login

Output:

PHP Math Functions

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>
Copy after login

Output:

PHP Math Functions

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>
Copy after login

Output:

PHP Math Functions

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>
Copy after login

Output:

PHP Math Functions

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.

  1. Install php5 or 5+
  2. Install localhost say WampServer etc
  3. Now create a file and name it as index.php
  4. Paste example snippet in the file created
  5. 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.

The above is the detailed content of PHP Math Functions. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
source:php
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!