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
?>
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
<?Php
echo max(array(3,15,-5)); //Output is 15
?>
$result = max(strlen('apple'), strlen('banana'), strlen('cherry'));
echo $result; // Output: 6
$array = [10, 50, 30, 90, 40];
echo max($array); // Output: 90
Read More on max value of Array
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.