PHP MATH FUNCTIONS

PHP Math: Abs() Function

The abs() function returns absolute value of given number. It returns an integer value but if you pass floating point value, it returns a float value.
Syntax
  1. number abs ( mixed $number )  
Example
  1. <?php  
  2. echo (abs(-7)."<br/>"); // 7 (integer)  
  3. echo (abs(7)."<br/>"); //7 (integer)  
  4. echo (abs(-7.2)."<br/>"); //7.2 (float/double)  
  5. ?>  
Output:
7 
7 
7.2

PHP Math: Ceil() Function

The ceil() function rounds fractions up.
Syntax
  1. float ceil ( float $value )  
Example
  1. <?php  
  2. echo (ceil(3.3)."<br/>");// 4  
  3. echo (ceil(7.333)."<br/>");// 8  
  4. echo (ceil(-4.8)."<br/>");// -4  
  5. ?>  
Output:
4
8
-4

PHP Math: Floor() Function

The floor() function rounds fractions down.
Syntax
  1. float floor ( float $value )  
Example
  1. <?php  
  2. echo (floor(3.3)."<br/>");// 3  
  3. echo (floor(7.333)."<br/>");// 7  
  4. echo (floor(-4.8)."<br/>");// -5  
  5. ?>  
Output:
3
7
-5

PHP Math: Sqrt() Function

The sqrt() function returns square root of given argument.
Syntax
  1. float sqrt ( float $arg )  
Example
  1. <?php  
  2. echo (sqrt(16)."<br/>");// 4  
  3. echo (sqrt(25)."<br/>");// 5  
  4. echo (sqrt(7)."<br/>");// 2.6457513110646  
  5. ?>  
Output:
4
5
2.6457513110646

PHP Math: Decbin() Function

The decbin() function converts decimal number into binary. It returns binary number as a string.
Syntax
  1. string decbin ( int $number )  
Example
  1. <?php  
  2. echo (decbin(2)."<br/>");// 10  
  3. echo (decbin(10)."<br/>");// 1010  
  4. echo (decbin(22)."<br/>");// 10110  
  5. ?>  
Output:
10
1010
10110

PHP Math: Dechex() Function

The dechex() function converts decimal number into hexadecimal. It returns hexadecimal representation of given number as a string.
Syntax
  1. string dechex ( int $number )  
Example
  1. <?php  
  2. echo (dechex(2)."<br/>");// 2  
  3. echo (dechex(10)."<br/>");// a  
  4. echo (dechex(22)."<br/>");// 16  
  5. ?>  
Output:
2
a
16

PHP Math: Decoct() Function

The decoct() function converts decimal number into octal. It returns octal representation of given number as a string.
Syntax
  1. string decoct ( int $number )  
Example
  1. <?php  
  2. echo (decoct(2)."<br/>");// 2  
  3. echo (decoct(10)."<br/>");// 12  
  4. echo (decoct(22)."<br/>");// 26  
  5. ?>  
Output:
2
12
26

PHP Math: Base_convert() Function

The base_convert() function allows you to convert any base number to any base number. For example, you can convert hexadecimal number to binary, hexadecimal to octal, binary to octal, octal to hexadecimal, binary to decimal etc.
Syntax
  1. string base_convert ( string $number , int $frombase , int $tobase )  
Example
  1. <?php  
  2. $n1=10;  
  3. echo (base_convert($n1,10,2)."<br/>");// 1010  
  4. ?>  
Output:
1010

PHP Math: Bindec() Function

The bindec() function converts binary number into decimal.
Syntax
  1. number bindec ( string $binary_string )  
Example
  1. <?php  
  2. echo (bindec(10)."<br/>");// 2  
  3. echo (bindec(1010)."<br/>");// 10  
  4. echo (bindec(1011)."<br/>");// 11  
  5. ?>  
Output:
2
10
11

Comments

Popular posts from this blog

Query to get concurrent Program details with Parameter list and value set attached to parameter.

Japan Style: Architecture Interiors Design

AR and OM link in oracle apps / AR transaction and Sales Order join query