PHP Math
PHP Math
PHP has a set of math functions that allows you to perform mathematical tasks on numbers.
PHP pi( ) Function
The pi( ) function returns the value of PI:
However; all variable names are case-sensitive!
<?php
echo(pi()); // returns 3.1415926535898
?>
PHP min( ) and max( ) Functions
The min( ) and max( ) functions can be used to find the lowest or highest value in a list of arguments:
<?php
echo(min(0, 150, 30, 20, -8, -200)); // returns -200
echo(max(0, 150, 30, 20, -8, -200)); // returns 150
?>
PHP abs() Function
The abs( ) function returns the absolute (positive) value of a number:
>?php
echo(abs(-6.7)); // returns 6.7
?>
PHP sqrt() Function
The sqrt( ) function returns the square root of a number:
<?php
echo(sqrt(64)); // returns 8
?>
PHP round( ) Function
The round( ) function rounds a floating-point number to its nearest integer:
<?php
echo(round(0.60)); // returns 1
echo(round(0.49)); // returns 0
?>
Random Numbers
The rand( ) function generates a random number.
<?php
echo(rand());
?>
To get more control over the random number, you can add the optional min and max parameters to specify the lowest integer and the highest integer to be returned.
<?php
echo(rand(10, 100));
?>
Convert Decimal Numbers to Binary and Vice Versa
The decbin( ) function is used to convert a decimal number into binary number. Whereas its counterpart the bindec() function converts a number from binary to decimal.
<?php
// Convert Decimal to Binary
echo decbin(2); // 0utputs: 10
echo decbin(12); // 0utputs: 1100
echo decbin(100); // 0utputs: 1100100
// Convert Binary to Decimal
echo bindec(10); // 0utputs: 2
echo bindec(1100); // 0utputs: 12
echo bindec(1100100); // 0utputs: 100
?>
Convert Decimal Numbers to Hexadecimal and Vice Versa
The dechex( ) function is used to convert a decimal number into hexadecimal representation. Whereas, the hexdec() function is used to converts a hexadecimal string to a decimal number.
<?php
// Convert decimal to hexadecimal
echo dechex(255); // 0utputs: ff
echo dechex(196); // 0utputs: c4
echo dechex(0); // 0utputs: 0
// Convert hexadecimal to decimal
echo hexdec('ff'); // 0utputs: 255
echo hexdec('c4'); // 0utputs: 196
echo hexdec(0); // 0utputs: 0
?>
Convert Decimal Numbers to Octal and Vice Versa
The decoct( ) function is used to convert a decimal number into octal representation. Whereas, the octdec() function is used to converts a octal number to a decimal number.
<?php
// Convert decimal to octal
echo decoct(12); // 0utputs: 14
echo decoct(256); // 0utputs: 400
echo decoct(77); // 0utputs: 115
// Convert octal to decimal
echo octdec('14'); // 0utputs: 12
echo octdec('400'); // 0utputs: 256
echo octdec('115'); // 0utputs: 77
?>
Convert a Number from One Base System to Another
The base_convert( ) function can be used to convert a number from one base system to other. For example, you can convert decimal (base 10) to binary (base 2), hexadecimal (base 16) to octal (base 8), octal to hexadecimal, hexadecimal to decimal, and so on.
This function accepts three parameters: the number to convert, the base it's currently in, and the base it's to be converted to. The basic syntax is as follows: base_convert(number, frombase, tobase);
Here, the number can be either an integer or a string representing an integer. Both frombase and tobase have to be between 2 and 36, inclusive. Digits in numbers with a base higher than 10 will be represented with the letters a-z, where a means 10, b means 11 and z means 35. Here's a simple example to show how this function works:
<?php
// Convert decimal to binary
echo base_convert('12', 10, 2); // 0utputs: 1100
// Convert binary to decimal
echo base_convert('1100', 2, 10); // 0utputs: 12
// Convert decimal to hexadecimal
echo base_convert('10889592', 10, 16); // 0utputs: a62978
// Convert hexadecimal to decimal
echo base_convert('a62978', 16, 10); // 0utputs: 10889592
// Convert decimal to octal
echo base_convert('82', 10, 8); // 0utputs: 122
// Convert octal to decimal
echo base_convert('122', 8, 10); // 0utputs: 82
// Convert hexadecimal to octal
echo base_convert('c2c6a8', 16, 8); // 0utputs: 60543250
// Convert octal to hexadecimal
echo base_convert('60543250', 8, 16); // 0utputs: c2c6a8
// Convert octal to binary
echo base_convert('42', 8, 2); // 0utputs: 100010
// Convert binary to octal
echo base_convert('100010', 2, 8); // 0utputs: 42
// Convert hexadecimal to binary
echo base_convert('abc', 16, 2); // 0utputs: 101010111100
// Convert binary to hexadecimal
echo base_convert('101010111100', 2, 16); // 0utputs: abc
?>
The base_convert() function will always return a string value. If the returned value is in base 10 the resulting string can be used as a numeric string in calculations and PHP will convert it to a number when the calculation is performed.