← Back to Chapters

PHP Math min() & max()

? PHP Math min() & max()

? Quick Overview

The min() and max() functions in PHP are used to determine the smallest and largest values from a group of values or an array. They are commonly used in calculations, data validation, and analytics.

? Key Concepts

  • min() finds the lowest value
  • max() finds the highest value
  • Works with arrays or comma-separated values

? Syntax / Theory

  • min(array)
  • max(array)
  • min(value1, value2, ...)
  • max(value1, value2, ...)

? Code Example: min()

? View Code Example
// Finding the smallest value in an array
<?php
$values = array(5, 10, 15, 20, 25);
$min_value = min($values);
echo $min_value;
?>

? Code Example: max()

? View Code Example
// Finding the largest value in an array
<?php
$values = array(5, 10, 15, 20, 25);
$max_value = max($values);
echo $max_value;
?>

? Live Output / Explanation

The first example outputs 5 because it is the smallest value. The second example outputs 25 because it is the largest value in the array.

? Interactive Concept

You can dynamically test different values by modifying the array numbers and observing how the output changes instantly.

? Use Cases

  • Finding highest scores or lowest prices
  • Validating minimum or maximum limits
  • Analyzing numeric datasets

✅ Tips & Best Practices

  • Always ensure the array is not empty
  • Use numeric-only arrays for predictable results
  • Can be combined with sorting functions

? Try It Yourself

  • Create an array with random numbers and test both functions
  • Use floats and integers together
  • Pass values directly without an array