Math Functions in PHP: Part 4


Introduction

The PHP math functions are used to handle values within the range of integer and float types. In this article I describe some of the PHP math functions. For
some other math functions, see:

  1. Math Function in PHP:Part 1
  2. Math Functions in PHP:Part 2 
  3. Math Functions in PHP:Part 3 
The PHP math decbin function is used to convert a decimal number to binary number.

Syntax
decbin  (DecimalNumber)


Parameters in

Decbin function

It takes one parameter; it is:
 

Parameter Description
DecimalNumber It specifies decimal number to convert.


Example of Decbin function

 

The following example of the Decbin function converts decimal numbers to binary numbers. The decimal numbers "10", "4", "1034" and "9999" are converted to binary numbers.


 

<?php

$val1=decbin(10);

$val2=decbin(4);

$val3=decbin(1034);

$val4=decbin(9999);

echo "Binary value of <b>10</b> is <b>$val1</b> <br />";

echo "Binary value of <b>4</b> is <b>$val2</b> <br />";

echo "Binary value of <b>1034</b> is <b>$val3</b> <br />";

echo "Binary value of <b>9999</b> is <b>$val4</b> <br />";?>


Output

math-decbin-function-in-php.jpg


PHP Dechex() function

The PHP math dechex function is used to convert a decimal number to a hexadecimal number.

Syntax

dechex  (DecimalNumber)


Parameters in
Dechex function

It takes one parameter; it is:
 

Parameter Description
DecimalNumber It specifies decimal number to convert.

Example of Dechex function

 

The following sample of the Dechex function converts decimal numbers to a hexadecimal numbers. The decimal numbers "10", "4", "1034" and "9999" are converted to hexadecimal numbers.

 

<?php

$val1=dechex(10);

$val2=dechex(4);

$val3=dechex(1034);

$val4=dechex(9999);

echo "Hexadecimal value of <b>10</b> is <b>$val1</b> <br />";

echo "Hexadecimal value of <b>4</b> is <b>$val2</b> <br />";

echo "Hexadecimal value of <b>1034</b> is <b>$val3</b> <br />";

echo "Hexadecimal value of <b>9999</b> is <b>$val4</b> <br />";

?>



Output


math-dechex-function-in-php.jpg

PHP Decoct() function

The PHP math  decoct function is used to converts a decimal number to an octal number.

Syntax

decoct  (DecimalNumber)


Parameters in
Decoct function

It takes one parameter.
 

Parameter Description
DecimalNumber It specifies decimal number to convert.

Example of Decoct function

 

The following sample of the Decoct function converts decimal numbers to octal numbers. The decimal numbers "10", "4", "1034" and "9999" are converted to octal numbers.

 

<?php

$val1=decoct(10);

$val2=decoct(4);

$val3=decoct(1034);

$val4=decoct(9999);

echo "Octal value of <b>10</b> is <b>$val1</b> <br />";

echo "Octal value of <b>4</b> is <b>$val2</b> <br />";

echo "Octal value of <b>1034</b> is <b>$val3</b> <br />";

echo "Octal value of <b>9999</b> is <b>$val4</b> <br />";

?>


Output

math-decoct-function-in-php.jpg

PHP Deg2rad() function

The PHP math deg2rad function is used to converts a number in degree to its radian number.

Syntax

deg2rad  (DegreeNumber)


Parameters in
Deg2rad function

It takes one parameter; it is:
 

Parameter Description
DegreelNumber It specifies decimal degree to convert.

Example of Der2rad function

 

The following sample of the Der2rad function converts numbers in degrees to radians. The degrees "10", "4", "1034" and "9999" are converted to radians.

 

<?php

$val1=deg2rad(10);

$val2=deg2rad(4);

$val3=deg2rad(1034);

$val4=deg2rad(9999);

echo "Radian value of <b>10</b> is <b>$val1</b> <br />";

echo "Radian value of <b>4</b> is <b>$val2</b> <br />";

echo "Radian value of <b>1034</b> is <b>$val3</b> <br />";

echo "Radian value of <b>9999</b> is <b>$val4</b> <br />";

?>



Output

math-deg2rad-function-in-php.jpg

Up Next
    Ebook Download
    View all
    Learn
    View all