clamp
clamp(mixed $value, mixed $min, mixed $max): mixed
Range clamping (new in PHP 8.6): returns the value when it lies within [min, max], otherwise the nearest bound; min > max throws ValueError.
C C23
#include <stdio.h>
/* No clamping primitive in the C stdlib — the classic hand-rolled ternary idiom */
static int clamp(int value, int min, int max) {
if (value < min) return min;
if (value > max) return max;
return value;
}
int main(void) {
printf("%d\n", clamp(55, 0, 100));
printf("%d\n", clamp(-5, 0, 100));
printf("%d\n", clamp(103, 0, 100));
/* No guard for min > max: silently returns min (5 < 10) — hand-rolled means self-policed */
printf("%d\n", clamp(5, 10, 1));
return 0;
}
55 0 100 10
C++ C++26
#include <algorithm>
#include <iostream>
int main() {
// std::clamp (C++17 <algorithm>): the native counterpart of PHP's clamp
std::cout << std::clamp(55, 0, 100) << "\n";
std::cout << std::clamp(-5, 0, 100) << "\n";
std::cout << std::clamp(103, 0, 100) << "\n";
// Precondition lo <= hi: violating it is UB, not an exception — std::clamp never checks
// std::clamp(5, 10, 1); // UB — deliberately not demonstrated
return 0;
}
55 0 100
C# v11.0.100
// Math.Clamp (.NET Core 2.0+): the native counterpart of PHP's clamp
Console.WriteLine(Math.Clamp(55, 0, 100));
Console.WriteLine(Math.Clamp(-5, 0, 100));
Console.WriteLine(Math.Clamp(103, 0, 100));
// min > max throws ArgumentException — same guard-first camp as PHP
try
{
Math.Clamp(5, 10, 1);
}
catch (ArgumentException e)
{
Console.WriteLine(e.GetType().Name);
}
55 0 100 ArgumentException
Go v1.27.4
package main
import "fmt"
func main() {
// Go has no clamp builtin — Go 1.21's built-in min/max make the squeeze idiom clean
fmt.Println(max(0, min(55, 100)))
fmt.Println(max(0, min(-5, 100)))
fmt.Println(max(0, min(103, 100)))
// min > max: the idiom silently returns min (10) — no guard, you police the order yourself
fmt.Println(max(10, min(5, 1)))
}
55 0 100 10
Java v27.0.1
public class Main {
public static void main(String[] args) {
// Math.clamp arrived only in Java 21 (2023) — a latecomer among built-ins
System.out.println(Math.clamp(55, 0, 100));
System.out.println(Math.clamp(-5, 0, 100));
System.out.println(Math.clamp(103, 0, 100));
// min > max throws IllegalArgumentException — guard-first camp like PHP
try {
Math.clamp(5, 10, 1);
} catch (IllegalArgumentException e) {
System.out.println(e.getClass().getSimpleName());
}
}
}
55 0 100 IllegalArgumentException
JavaScript v26.13.1
// No clamp in JS — the classic Math idiom: min(max(v, lo), hi)
console.log(Math.min(Math.max(55, 0), 100));
console.log(Math.min(Math.max(-5, 0), 100));
console.log(Math.min(Math.max(103, 0), 100));
// min > max: the idiom silently returns hi (1) — order not policed
console.log(Math.min(Math.max(5, 10), 1));
55 0 100 1
PHP v8.6.0
// clamp (PHP 8.6+): squeeze $value into [min, max] — below returns min, above returns max
var_dump(clamp(55, 0, 100));
var_dump(clamp(-5, 0, 100));
var_dump(clamp(103, 0, 100));
var_dump(clamp(2.5, 1, 10));
// Works on any comparable values, strings included
echo clamp("J", "A", "F"), "\n";
// min > max throws ValueError — no silent inversion
try {
clamp(5, 10, 1);
} catch (ValueError $e) {
echo get_class($e), "\n";
}
int(55) int(0) int(100) float(2.5) F ValueError
Python v3.15.2
# No clamp builtin — the classic squeeze idiom: max(lo, min(v, hi))
print(max(0, min(55, 100)))
print(max(0, min(-5, 100)))
print(max(0, min(103, 100)))
# min > max: the idiom silently returns lo (10) — order not policed
print(max(10, min(5, 1)))
55 0 100 10
Rust v1.99.1
fn main() {
// Ord::clamp / f64::clamp (Rust 1.50+): native clamping, generic over Ord
println!("{}", 55.clamp(0, 100));
println!("{}", (-5).clamp(0, 100));
println!("{}", 103.clamp(0, 100));
// min > max panics — guard-first camp like PHP
std::panic::set_hook(Box::new(|_| {})); // silence the default panic print to keep stdout clean
let panicked = std::panic::catch_unwind(|| 5.clamp(10, 1)).is_err();
println!("min>max panicked: {}", panicked);
}
55 0 100 min>max panicked: true
TypeScript v26.13.1
// No clamp in TS — wrap the classic Math idiom once, then use it everywhere
const clamp = (v: number, lo: number, hi: number): number => Math.min(Math.max(v, lo), hi);
console.log(clamp(55, 0, 100));
console.log(clamp(-5, 0, 100));
console.log(clamp(103, 0, 100));
// min > max: silently returns hi (1) — order not policed
console.log(clamp(5, 10, 1));
55 0 100 1
Language differences
The behavior matrix is this entry's teaching core: built-in APIs generally guard (throw), hand-rolled squeeze idioms generally swallow silently — an inverted min/max is the classic silent bug. Who throws and who swallows:
C: no built-in — hand-rolled ternary (example); no guard for min > max, silently returns min (10 in the example).
C++: std::clamp (C++17 <algorithm>) — lo > hi is a precondition violation = UB (no throw, no check, any behavior allowed), worse than silent swallowing; not demonstrated in the example.
C#: Math.Clamp (.NET Core 2.0+) — min > max throws ArgumentException (guard camp).
Go: no clamp builtin — with min/max built in since Go 1.21 the squeeze idiom max(lo, min(v, hi)) finally reads well; min > max silently returns lo (10).
Java: Math.clamp arrived only in Java 21 (2023) — a latecomer builtin; min > max throws IllegalArgumentException (guard camp).
JavaScript: no builtin — the idiom Math.min(Math.max(v, lo), hi); min > max silently returns hi (1) — nesting order is opposite to Python's, so it swallows differently too.
PHP: clamp is new in 8.6 (2026-11-19); min > max throws ValueError (NaN bounds throw too); any comparable values work (strings, DateTimeImmutable) and the original type is preserved.
Python: no builtin — the idiom max(lo, min(v, hi)); min > max silently returns lo (10).
Rust: native on both fronts — Ord::clamp (1.50+) and f64::clamp; min > max panics (guard camp); the f64 variant panics on NaN bounds too.
TypeScript: same as JavaScript (silent idiom).