Difference Between Min and Max Function in PHP

Introduction

In this article I will explain the min and max functions in PHP. Both functions are available as math functions. I will discuss the min() function first.

The "min()" function returns the number with the lowest value.

Syntax

min($value)

 

Parameter Description
$value  Returns the lowest value

Example

The following example shows how to use the min() function:

<?php

echo min(2, 3, 1, 6, 7)."<br>";

echo min(array(2, 4, 5))."<br>";

echo min(0, 'Hi..Gaye')."<br>";   

echo min('Hi..Gaye', 0)."<br>";    

echo min('Hi..Gaye', -1)."<br>";   

$value = min(array(2, 4, 8), array(2, 5, 1));

$value = min('string', array(2, 5, 7), 42);  

?>

Output

min() image.jpg

The "max()" function returns the number with the highest value.

Syntax

max($value)

 

Parameter Description
$value  Returns the highest value

Example

The following example shows how to use the max() function:

<?php

echo max(1, 3, 5, 6, 7)."<br>"

echo max(array(2, 4, 5))."<br>";

echo max(0, 'Hi..Dude')."<br>";    

echo max('Hi..Dude', 0)."<br>";    

echo max('42', 3)."<br>";

echo max(-1, 'Hi..Dude')."<br>"

$val = max(array(2, 2, 2), array(1, 1, 1, 1));

$val = max(array(2, 4, 8), array(2, 5, 7));

$val = max('string', array(2, 5, 7), 42);  

?>

Output 

max() function.jpg


Similar Articles