PHP includes various pre-built functions designed to assist you in executing tasks ranging from basic arithmetic like addition and subtraction to more complex computations. Earlier, you learned how to conduct elementary mathematical operations in the PHP operators section. Here’s another illustrative instance:
<?php
echo 7 + 3; // 0utputs: 10
echo 7 - 2; // 0utputs: 5
echo 7 * 2; // 0utputs: 14
echo 7 / 2; // 0utputs: 3.5
echo 7 % 2; // 0utputs: 1
?>
Each mathematical operation follows a specific order of precedence; typically, multiplication and division take precedence over addition and subtraction. Yet, parentheses can override this order; any expression within parentheses is evaluated first, regardless of the usual precedence rules. This is illustrated in the following example:
<?php
echo 5 + 4 * 10; // 0utputs: 45
echo (5 + 4) * 10; // 0utputs: 90
echo 5 + 4 * 10 / 2; // 0utputs: 25
echo 8 * 10 / 4 - 2; // 0utputs: 18
echo 8 * 10 / (4 - 2); // 0utputs: 40
echo 8 + 10 / 4 - 2; // 0utputs: 8.5
echo (8 + 10) / (4 - 2); // 0utputs: 9
?>
In the next section, we'll explore some essential PHP functions commonly used for mathematical tasks.
The absolute value of an integer or a float can be determined using the abs()
function. Here's an example to illustrate:
<?php
echo abs(5); // 0utputs: 5 (integer)
echo abs(-5); // 0utputs: 5 (integer)
echo abs(4.2); // 0utputs: 4.2 (double/float)
echo abs(-4.2); // 0utputs: 4.2 (double/float)
?>
When using the abs()
function, negative numbers return positive values, while positive numbers remain unchanged.
The ceil()
function rounds up a decimal value to the nearest integer, whereas the floor()
function rounds it down to the nearest integer. Here's an example to illustrate:
<?php
// Round fractions up
echo ceil(4.2); // 0utputs: 5
echo ceil(9.99); // 0utputs: 10
echo ceil(-5.18); // 0utputs: -5
// Round fractions down
echo floor(4.2); // 0utputs: 4
echo floor(9.99); // 0utputs: 9
echo floor(-5.18); // 0utputs: -6
?>
To find the square root of a positive number, you can utilize the sqrt()
function. For negative numbers, this function returns a special value NAN
. Here’s an example:
<?php
echo sqrt(9); // 0utputs: 3
echo sqrt(25); // 0utputs: 5
echo sqrt(10); // 0utputs: 3.1622776601684
echo sqrt(-16); // 0utputs: NAN
?>
The rand()
function is used for generating random numbers. You can specify a range by providing minimum and maximum arguments, as demonstrated in this example:
<?php
// Generate some random numbers
echo rand() . "<br>";
echo rand() . "<br>";
// Generate some random numbers between 1 and 10 (inclusive)
echo rand(1, 10) . "<br>";
echo rand(1, 10) . "<br>";
?>
If you call the rand()
function without specifying the optional min
and max
arguments, it returns a pseudo-random number between 0
and getrandmax()
. On Windows platforms, getrandmax()
indicates the largest possible random value, which is limited to 32767. If you need a range greater than 32767, you should specify both the min
and max
arguments.
The decbin()
function converts a decimal number into its binary representation, while the bindec()
function performs the reverse, converting binary numbers back 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
?>
The dechex()
function converts a decimal number into its hexadecimal representation. Conversely, the hexdec()
function converts a hexadecimal string back into 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
?>
The decoct()
function converts a decimal number into its octal representation. On the other hand, the octdec()
function converts an octal number into 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
?>
The base_convert()
function allows you to convert a number from one base system to another. For instance, you can convert from decimal (base 10) to binary (base 2), hexadecimal (base 16) to octal (base 8), octal to hexadecimal, hexadecimal to decimal, and more.
This function requires three parameters: the number to convert, its current base, and the target base. The basic syntax is:
The number
can be an integer or a string representing an integer. Both frombase
and tobase
must be integers between 2 and 36, inclusive. If a base is greater than 10, digits are represented by letters from a to z, where a is 10, b is 11, up to z which is 35. Here's a straightforward example to demonstrate how this function operates:
<?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
?>
Note: The base_convert()
function always returns a string value. If the resulting string represents a base 10 number, PHP treats it as a numeric string in calculations and converts it to a number during computation.