max() / min() / rand() / random_int() Since: PHP 4(2000)
Gets maximum and minimum values, and generates random numbers. You can find extreme values from an array or multiple arguments, and generate cryptographically secure random numbers for security-sensitive use cases.
Syntax
// Returns the maximum value. Accepts an array or multiple values as arguments. max($value1, $value2, ...); // Returns the minimum value. Accepts an array or multiple values as arguments. min($value1, $value2, ...); // Generates a pseudo-random number. rand($min, $max); // Generates a pseudo-random number using the Mersenne Twister algorithm. mt_rand($min, $max); // Generates a cryptographically secure random integer. random_int($min, $max);
Function List
| Function | Description |
|---|---|
| max($value1, $value2, ...) | Returns the largest value among the arguments. If an array is passed, returns the maximum value in the array. |
| min($value1, $value2, ...) | Returns the smallest value among the arguments. If an array is passed, returns the minimum value in the array. |
| rand($min, $max) | Returns a pseudo-random integer within the specified range. If no arguments are provided, returns a value between 0 and the result of getrandmax(). |
| mt_rand($min, $max) | Returns a pseudo-random number using the Mersenne Twister algorithm. Higher quality than rand(), but not suitable for cryptographic use. |
| random_int($min, $max) | Returns a cryptographically secure random integer. Added in PHP 7. Use this for security-sensitive operations such as token generation and password resets. |
Return Value
max() and min() return the largest or smallest value among the arguments, preserving the original type. All random number functions return an integer within the specified range. random_int() throws an exception if generation fails.
Sample Code
<?php // Get the maximum value using max(). echo max(10, 25, 3); // Outputs '25'. echo max(-5, -1, -10); // Outputs '-1'. // Get the maximum and minimum values from an array. $scores = [85, 92, 78, 96, 88]; echo max($scores); // Outputs '96'. echo min($scores); // Outputs '78'. // Practical example: use min() to enforce a lower bound. $quantity = -3; $safe_quantity = max(0, $quantity); // Clamps the negative value to 0, outputs '0'. echo $safe_quantity; // Practical example: clamp a value within a range. $page = 150; $page = max(1, min($page, 100)); // Clamps to the range 1–100, outputs '100'. echo $page; // Generate a pseudo-random number using rand(). echo rand(1, 6); // Outputs a random integer between 1 and 6. // Generate a pseudo-random number using the Mersenne Twister algorithm. echo mt_rand(1, 100); // Outputs a random integer between 1 and 100. // Generate a cryptographically secure random number using random_int(). echo random_int(100000, 999999); // Outputs a secure 6-digit random number. // Practical example: generate a secure token. $token = random_int(0, PHP_INT_MAX); echo $token; // Outputs an unpredictable random integer.
Notes
max() and min() find the extreme values from an array or multiple arguments. They are commonly used to enforce upper and lower bounds — for example, max(0, $value) clamps a value to zero or above, and max(1, min($value, 100)) restricts a value to a specific range.
rand() and mt_rand() are not suitable for security-sensitive use. When generating values that must be unpredictable — such as password reset tokens or CSRF tokens — always use random_int(). It uses the operating system's cryptographically secure random number generator, making its output computationally infeasible to predict.
For generating random strings, random_bytes() can be used to work with raw byte sequences. For rounding numbers, see round() / ceil() / floor().
If you find any errors or copyright issues, please contact us.