Contents
PHP Math Functions Complete Reference
PHP Math Functions Complete Reference
PHP Math Functions
PHP includes a range of predefined mathematical functions that facilitate operations on integer and float types. These functions are integrated into the PHP core and require no installation.
Example: Using the decbin()
function in PHP:
abs()
Description: Return the absolute (positive) value of a number.
Example:
echo abs(-10);
// Output: 10
acos()
Description: Find the arc cosine of a number, returning the result in radians.
Example:
echo acos(0.5);
// Output: 1.0471975511966
acosh()
Description: Calculate the inverse hyperbolic cosine of a number.
Example:
echo acosh(2);
// Output: 1.3169578969248
asin()
Description: Calculate the arc sine of a number in radians.
Example:
echo asin(0.5);
// Output: 0.5235987755983
asinh()
Description: Find the inverse hyperbolic sine of a number
Example:.
echo asinh(1);
// Output: 0.88137358701954
atan2()
Description: Calculate the arc tangent of two numbers x and y.
Example:
echo atan2(1, 1);
// Output: 0.78539816339745
atan()
Description: Return the arc tangent of a number, result in radians.
Example:
echo atan(1);
// Output: 0.78539816339745
atanh()
Description: Find the inverse hyperbolic tangent of a number.
Example:
$file = fopen("example.txt", "r");
echo fgets($file); // Outputs one line
fclose($file);
base_convert()
Description: Convert a number from one base to another.
Example:
echo base_convert("A1", 16, 2);
// Output: 10100001
bindec()
Description: Convert a binary string to its decimal equivalent.
Example:
echo bindec("1101");
// Output: 13
ceil()
Description: Round a number up to the nearest integer.
Example:
echo ceil(4.3);
// Output: 5
cos()
Description: Find the cosine of a number (in radians).
Example:
echo cos(1);
// Output: 0.54030230586814
cosh()
Description: Calculate the hyperbolic cosine of a number.
Example:
echo cosh(1);
// Output: 1.5430806348152
decbin()
Description: Convert a decimal number to binary.
Example:
echo decbin(12);
// Output: 1100
dechex()
Description: Convert a decimal number to hexadecimal.
Example:
echo dechex(255);
// Output: ff
decoct()
Description: Convert a decimal number to octal.
Example:
echo decoct(15);
// Output: 17
deg2rad()
Description: Convert degrees to radians.
Example:
echo deg2rad(180);
// Output: 3.1415926535898
exp()
Description: Calculate e raised to the power of a given number.
Example:
echo exp(2);
// Output: 7.3890560989306
expm1()
Description: Calculate e raised to the power of a given number minus one.
Example:
echo expm1(1);
// Output: 1.718281828459
hypot()
Description: Returns the square root of the sum of squares of two numbers.
Example:
echo hypot(3, 4);
// Output: 5
fmod()
Description: Calculate the remainder of a division (modulo operation).
Example:
echo fmod(5.7, 1.3);
// Output: 0.5
hexdec()
Description: Convert a hexadecimal string to its decimal equivalent.
Example:
echo hexdec("ff");
// Output: 255
hypot()
Description: Calculate the length of the hypotenuse of a right-angled triangle.
Example:
echo hypot(3, 4);
// Output: 5
intdiv()
Description: Calculate the integer quotient of the division of two numbers.
Example:
echo intdiv(7, 3);
// Output: 2
is_finite()
Description: Check if a number is finite.
Example:
echo is_finite(1.2e308) ? "Finite" : "Infinite";
// Output: Finite
is_infinite()
Description: Check if a number is infinite.
Example:
echo is_infinite(1.2e308 * 10) ? "Infinite" : "Finite";
// Output: Infinite
is_nan()
Description: Check if a value is ‘not a number’.
Example:
echo is_nan(acos(2)) ? "Not a number" : "Number";
// Output: Not a number
is_readable()
Description: Determine if a file exists and is readable.
Example:
echo is_readable("example.txt") ? "Readable" : "Not readable";
// Output: Readable or Not readable
is_uploaded_file()
Description: Check if a file was uploaded via HTTP POST.
Example:
echo is_uploaded_file($_FILES['file']['tmp_name']) ? "Uploaded" : "Not uploaded";
// Output: Uploaded or Not uploaded
is_writable()
Description: Verify if a file is writable.
Example:
echo is_writable("example.txt") ? "Writable" : "Not writable";
// Output: Writable or Not writable
link()
Description: Create a hard link for a specified target.
Example:
link("target.txt", "hardlink.txt");
// Output: (creates a hard link 'hardlink.txt' pointing to 'target.txt')
lstat()
Description: Retrieve information about a file or symbolic link.
Example:
print_r(lstat("example.txt"));
// Output: (array of file or symbolic link information)
mkdir()
Description: Create a new directory with the specified path.
Example:
mkdir("new_folder");
// Output: (creates 'new_folder' directory)
pathinfo()
Description: Get information about a path as an associative array or string.
Example:
print_r(pathinfo("example.txt"));
// Output: (displays path info, e.g., directory, basename, extension)
pclose()
Description: Close a pipe opened by popen()
.
Example:
$pipe = popen("ls", "r");
pclose($pipe);
// Output: (closes the pipe to the command 'ls')
popen()
Description: Open a pipe to a specified command.
Example:
$pipe = popen("ls", "r");
// Output: (opens a pipe to list directory contents)
readfile()
Description: Read a file and output it directly to the buffer.
Example:
readfile("example.txt");
// Output: (displays contents of 'example.txt')
realpath()
Description: Return the canonical absolute pathname.
Example:
echo realpath("example.txt");
// Output: (full path to 'example.txt')
rewind()
Description: Set the file pointer to the beginning of a file.
Example:
$file = fopen("example.txt", "r");
rewind($file);
fclose($file);
// Output: (moves file pointer to start)
stat()
Description: Retrieve detailed information about a file.
Example:
print_r(stat("example.txt"));
// Output: (array of file statistics)
symlink()
Description: Create a symbolic link pointing to a specified target.v
Example:
symlink("target.txt", "symbolic_link.txt");
// Output: (creates symbolic link 'symbolic_link.txt' to 'target.txt')
tmpfile()
Description: Create a temporary file in read-write mode.
Example:
$temp = tmpfile();
fwrite($temp, "Temporary data");
fclose($temp);
// Output: (creates and closes a temporary file)
touch()
Description: Set the access and modification times for a file.
Example:
touch("example.txt");
// Output: (updates 'example.txt' timestamp)