Example 1 — Basic clamp() usage
This example limits numbers to the range 0 to 10.
<?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
- 15 is above the maximum 10, so clamp() returns 10.
- -5 is below the minimum 0, so clamp() returns 0.
- 5 is already between 0 and 10, so it is returned unchanged.
- Each result is printed on its own line using PHP_EOL.

