Home >>PHP Tutorial >PHP Numeric Functions

PHP Numeric Functions

PHP numeric functions

Don't think that PHP's power is limited to strings only.

The language has over 50 built-in functions for working with numbers, ranging from simple formatting functions to functions for arithmetic, logarithmic, and trigonometric manipulations.

Some of these important functions are
Sr. No Function What it Does
1 ceil() Rounds a number up
2 floor() Rounds a number down
3 abs() Finds the absolute value of anumber
4 pow() Raises one number to the power of another
5 exp() Finds the exponent of a number
6 rand() Generates a random number
7 bindec() Converts a number from binary to decimal
8 decbin() Converts a number from decimal to binary
9 decoct() Converts a number from decimal to octal
10 octdec() Converts a number from octal to decimal
11 dechex() Converts a number from decimal to hexadecimal
12 hexdec() Converts a number from hexadecimal to decimal
13 number_format() Formats number with grouped thousands and decimals
14 printf() Formats a number using a custom specification
15 round find round number
16 sqrt find square root of a number

Eg i(ceil)
<?php
       
$num=19.7
       
echo ceil($num);
  
?>

Output 20
In the above example Initialize variable $num with value=19.7 , output will become 20. because this function round value up.
Eg ii(floor)
<?php
       
$num=19.7
       
echo floor($num);
  
?>

Output 19
in the above example variable $num = 19.7,and the output will become 19. Because this function round value down.
Eg iii(abs)
<?php
      
$num =-19.7
      
echo abs($num);
  
?>

Output 19
In the above example declare variable ($num) value=19.7 and the output will 19.7 Because abs( ) returns the absolute of given number.
Eg iv(pow)
<?php
     
echo pow(4,3);
  
?>

Output 64
In the above example. Pass pow( ) function inside echo with value(4,3). Its multiply (value=4). three times and the result is 64.
Eg v(rand)
<?php
    
echo rand(10,99);
  
?>

Output 55
In the above example Pass rand( ) function With value from( 10 to 99 ). it will display any random value lies from 10 to 100. when we refresh the page on every refresh it show random value like. 22,33 ,44,56 and so on.
Eg vi(bindec)
<?php

 echo bindec(1000);
  
?>

Output 8
In the above example bindec( ) function pass inside echo statement with binary value = 1000. So output will become 8 because bindec( ) function convert binary number into decimal number.
Eg vii(decbin)
<?php

 echo decbin(8);
 
?>

Output 1000
In the above example decbin( ) function Pass inside echo statement with decimal value = 8. So output will become. 1000

No Sidebar ads