wppaste
WordPress

clamp( mixed $value, mixed $min, mixed $max ): mixed

Since
7.1.0
Source
wp-includes/compat.php:715
Polyfill for clamp() function added in PHP 8.6.

Description

Clamps a value to be within the range of a given minimum and maximum.

If the value is within the bounds, the original value is returned.
If it is not within the bounds, the closest bound is returned.

Compatibility

WordPress
since 7.1.0
PHP
7.4–8.6-dev
  • 6.7.7
  • 6.8.8
  • 6.9.7
  • 7.0.4
  • 7.1.0

Present in 1 of the 5 tracked releases, added in 7.1.0, and compiles on PHP 7.4 through 8.6-dev.

Parameters

$valuemixed
The value to clamp.
$minmixed
The minimum bound. Must be less than or equal to $max.
$maxmixed
The maximum bound. Must be greater than or equal to $min.

Return value

mixed
The clamped value. Either $value, $min, or $max.

Performance profile

How much work a call to clamp() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.

Cost class
Trivial

Touches nothing outside its own arguments.

Scaling
Constant

No loop in the body: the same number of instructions runs whatever you pass in.

Instructions
7

Executed per call on PHP 8.5. The body compiles to 45.

Plugin surface
None

Nothing here hands control to plugin code.

Called by
2

2 places in core call this, so the cost is paid more often than your own code shows.

What one call costs · 1 distinct outcome

One number would be a lie: the work depends on which branch runs. These are every distinct cost clamp() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
always7none

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev4578
8.54578
8.445783 fewer instructions than PHP 8.3
8.348108
8.248108
8.148108
7.448108

An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.

Uses · 2

  • InvalidArgumentException::__construct()
  • ValueError::__construct()

Used by · 2

Source code

	function clamp( $value, $min, $max ) {		$throw_value_error = static function ( string $message ) {			// The ValueError class was introduced in PHP 8, so in 7.4 throw InvalidArgumentException instead.			if ( ! class_exists( 'ValueError', false ) ) {				throw new InvalidArgumentException( $message );			}			throw new ValueError( $message );		}; 		if ( is_float( $min ) && is_nan( $min ) ) {			$throw_value_error( 'clamp(): Argument #2 ($min) must not be NAN' );		} 		if ( is_float( $max ) && is_nan( $max ) ) {			$throw_value_error( 'clamp(): Argument #3 ($max) must not be NAN' );		} 		if ( $max < $min ) {			$throw_value_error( 'clamp(): Argument #2 ($min) must be smaller than or equal to argument #3 ($max)' );		} 		/*		 * The upper bound is checked before the lower bound to match the order in PHP's		 * implementation. Comparison in PHP is not transitive when operands of different		 * types are mixed (for example, comparing against a bool coerces both operands to		 * bool), so both bounds can compare as exceeded at once, and the first check wins.		 */		if ( $value > $max ) {			return $max;		} 		if ( $value < $min ) {			return $min;		} 		return $value;	}

Changelog

Introduced in 7.1.0.

Signature, return type and hooks compared across 1 parsed release.

About this page

Parsed data
Generated from the wordpress-develop 7.1.0 tag, from src/wp-includes/compat.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it.
Corrections
Something wrong on this page? Report it and it gets fixed in the next regeneration.