PHP Math

There are various built-in functions available for performing mathematical operations. Here's an overview of some of the common PHP math functions:


PHP pi() Function


pi() function returns the value of π (Pi), which is approximately 3.1415926535898.

Example
<!DOCTYPE html>
<html>
<body>

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

</body>
</html>

Try it yourself

PHP min() and max() Functions


The min() and max() functions can be used to returns the smallest or largest value in an array or a set of values.


Example
<!DOCTYPE html>
<html>
<body>

<?php
echo min(1, 2, 3, 4); // 1
echo max(1, 2, 3, 4); // 4

?>

</body>
</html>

Try it yourself

PHP abs() Function


The  abs()  function  returns the absolute value of a number.

Example
<!DOCTYPE html>
<html>
<body>

<?php
echo abs(-10); // 10
?>

</body>
</html>

Try it yourself

PHP pow() Function


The  pow()  function returns the base raised to the power of the exponent.

Example
<!DOCTYPE html>
<html>
<body>

<?php
echo pow(2, 3); // 8
?>

</body>
</html>

Try it yourself

PHP sqrt() Function


The  sqrt()  function  returns the square root of a number..

Example
<!DOCTYPE html>
<html>
<body>

<?php
echo sqrt(16); // 4
?>

</body>
</html>

Try it yourself

PHP round() Function


The  round()  function rounds a floating-point number.

Example
<!DOCTYPE html>
<html>
<body>

<?php
echo round(3.1459, 2); // 3.15
?>

</body>
</html>

Try it yourself

PHP floor() Function


The  floor()  function  rounds a number down to the nearest integer.

Example
<!DOCTYPE html>
<html>
<body>

<?php
echo floor(3.9); // 3
?>

</body>
</html>

Try it yourself

PHP ceil() Function


The  ceil()  function rounds a number up to the nearest integer.

Example
<!DOCTYPE html>
<html>
<body>

<?php
echo ceil(3.1); // 4
?>

</body>
</html>

Try it yourself

PHP rand() Function


The  rand()  function generates a random number.

Example
<!DOCTYPE html>
<html>
<body>

<?php
echo rand(); // Random number
?>

</body>
</html>

Try it yourself

However, you can also specify a range for the random number by passing two arguments: a minimum and maximum value.


Syntax


rand(int $min = 0, int $max = getrandmax())


  • $min: The minimum number in the range (inclusive). If not provided, it defaults to 0.
  • $max: The maximum number in the range (inclusive). If not provided, it defaults to getrandmax() (the largest possible random value).
Example
<!DOCTYPE html>
<html>
<body>

<?php
echo rand(1, 100); // Random number between 1 and 100
?>

</body>
</html>

Try it yourself

Whereisstuff is simple learing platform for beginer to advance level to improve there skills in technologies.we will provide all material free of cost.you can write a code in runkit workspace and we provide some extrac features also, you agree to have read and accepted our terms of use, cookie and privacy policy.
© Copyright 2024 www.whereisstuff.com. All rights reserved. Developed by whereisstuff Tech.