max(): Maximum number

Find the highest value with max()

max() returns the highest value from the supplied arguments or from an array. It is useful when a calculation needs an upper bound or largest measurement.

$scores = [72, 91, 84, 66];
echo max($scores); // 91

Use values of compatible types when possible. Mixing unrelated types can make comparisons harder to reason about.

echo max(4,8,2); // 8 
echo "<br>";
echo max(5,-1,0); // 5
echo "<br>";
echo max(2,0,-3); // 2
echo "<br>";
echo max(array(4,6,1)); // 6
echo "<br>";
Syntax
max($value1, $value2, ...)
We can get maximum value or highest number between two variables or two numbers by using max function. We can use more than two numbers also. Here is the code.
<?Php
echo max(5,25,6,-22,-66); // Output is 25 
?>

Comparing strings with max()

When all values are strings, max() compares them using PHP's normal string comparison rules. Avoid mixing strings and numbers in the same call because type juggling can make the result difficult to interpret.

$largest = max('apple', 'banana', 'cherry');
echo $largest; // cherry

Max with array

<?Php
echo max(array(3,15,-5)); //Output is 15
?>

Example: Finding Maximum String Length

$result = max(strlen('apple'), strlen('banana'), strlen('cherry'));
echo $result;  // Output: 6

Example: Maximum Value in an Array

$array = [10, 50, 30, 90, 40];
echo max($array);  // Output: 90
Type consistency: for predictable results, compare numbers with numbers or strings with strings rather than mixing unrelated types.

Read More on max value of Array


Math Functions min()
decbin() Binary equivalent of decimal number


Subscribe to our YouTube Channel here



plus2net.com











PHP video Tutorials
✖
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
© 2000-2026 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer