PHP clamp() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Math

Definition

The PHP clamp() function keeps a value inside a range. If the value is below the minimum it returns the minimum, and if it is above the maximum it returns the maximum.

It was added in PHP 8.4 and replaces the common pattern max($min, min($max, $value)) with one easy call.

Syntax

Call clamp() with the values you need. Start with this line.

PHP
$result = clamp($value, $min, $max);
CallWhat it does
clamp(15, 0, 10)Returns 10 because 15 is above the maximum.
clamp(-5, 0, 10)Returns 0 because -5 is below the minimum.
clamp(5, 0, 10)Returns 5 because it is already inside the range.

clamp() needs PHP 8.4 or newer. On older versions use max($min, min($max, $value)).

Library declarations
PHP
clamp(mixed $value, mixed $min, mixed $max): mixed

Parameters

ParameterDescription
valueRequired. The value you want to limit.
minRequired. The lowest allowed value. It must not be greater than $max.
maxRequired. The highest allowed value. It must not be smaller than $min.

Return Value

Return ValueDescription
mixed$min if $value is below it, $max if $value is above it, otherwise $value. The returned value keeps its original type.

PHP Examples

Example 1 — Basic clamp() usage

This example limits numbers to the range 0 to 10.

PHP
<?php
echo clamp(15, 0, 10) . PHP_EOL;
echo clamp(-5, 0, 10) . PHP_EOL;
echo clamp(5, 0, 10) . PHP_EOL;

How It Works

  1. 15 is above the maximum 10, so clamp() returns 10.
  2. -5 is below the minimum 0, so clamp() returns 0.
  3. 5 is already between 0 and 10, so it is returned unchanged.
  4. Each result is printed on its own line using PHP_EOL.

Example 2 — Wrong range and return types

clamp() throws a ValueError when the minimum is larger than the maximum. It also returns whichever limit it used, with that limit's own type.

PHP
<?php
try {
    echo clamp(5, 10, 0);
} catch (ValueError $e) {
    echo get_class($e) . PHP_EOL;
}

var_dump(clamp(1.5, 0, 1));
var_dump(clamp(0.5, 0, 1));

How It Works

  1. The range 10 to 0 is backwards because the minimum is bigger than the maximum.
  2. PHP throws a ValueError, and the catch block prints its class name.
  3. clamp(1.5, 0, 1) is above the maximum, so it returns the limit 1 as an int.
  4. clamp(0.5, 0, 1) is inside the range, so the original float 0.5 is returned.

Example 3 — Limit a volume level

A volume slider should never go below 0 or above 100. clamp() makes that rule very easy to write.

PHP
<?php
$levels = [-20, 35, 140];

foreach ($levels as $level) {
    $safe = clamp($level, 0, 100);
    echo "Level $level becomes $safe" . PHP_EOL;
}

How It Works

  1. The $levels array holds three values, including two invalid ones.
  2. clamp($level, 0, 100) limits each value to the range 0 to 100.
  3. -20 becomes 0 and 140 becomes 100, while 35 is unchanged.
  4. The safe value is printed with the original one for comparison.

Frequently Asked Questions

clamp() limits a value to a range. It returns the minimum if the value is too small, the maximum if it is too big, and the value itself if it is in between.
clamp() is available in PHP 8.4 and newer. It does not exist in older versions.
Use max($min, min($max, $value)). It gives the same result as clamp().
clamp() throws a ValueError because the range does not make sense.
No. It returns the value or the limit exactly as given. For example, clamp(1.5, 0, 1) returns the int 1 because that is the maximum you passed.

Next: cos()

cos() function returns the cosine of an angle, which is a number between -1 and 1.

cos() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful