Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions ext/standard/basic_functions.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -1606,6 +1606,12 @@ function min(mixed $value, mixed ...$values): mixed {}
*/
function max(mixed $value, mixed ...$values): mixed {}

/**
* @compile-time-eval
* @frameless-function {"arity": 3}
*/
function clamp(mixed $value, mixed $min, mixed $max): mixed {}

function array_walk(array|object &$array, callable $callback, mixed $arg = UNKNOWN): true {}

function array_walk_recursive(array|object &$array, callable $callback, mixed $arg = UNKNOWN): true {}
Expand Down
16 changes: 15 additions & 1 deletion ext/standard/basic_functions_arginfo.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

54 changes: 54 additions & 0 deletions ext/standard/math.c
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,60 @@ PHP_FUNCTION(round)
}
/* }}} */

#define PHP_CLAMP(zvalue, zmin, zmax) { \
if (EXPECTED(Z_TYPE_P(zmin) == IS_DOUBLE) && UNEXPECTED(zend_isnan(Z_DVAL_P(zmin)))) { \
zend_argument_value_error(2, "cannot be NAN"); \
RETURN_THROWS(); \
} \
\
if (EXPECTED(Z_TYPE_P(zmax) == IS_DOUBLE) && UNEXPECTED(zend_isnan(Z_DVAL_P(zmax)))) { \
zend_argument_value_error(3, "cannot be NAN"); \
RETURN_THROWS(); \
} \
\
if (zend_compare(zmax, zmin) == -1) { \
zend_argument_value_error(2, "must be smaller than or equal to argument #3 ($max)"); \
RETURN_THROWS(); \
} \
\
if (zend_compare(zmax, zvalue) == -1) { \
RETURN_COPY(zmax); \
} \
\
if (zend_compare(zvalue, zmin) == -1) { \
RETURN_COPY(zmin); \
} \
\
RETURN_COPY(zvalue); \
}

/* {{{ Return the given value if in range of min and max */
PHP_FUNCTION(clamp)
{
zval *zvalue, *zmin, *zmax;

ZEND_PARSE_PARAMETERS_START(3, 3)
Z_PARAM_ZVAL(zvalue)
Z_PARAM_ZVAL(zmin)
Z_PARAM_ZVAL(zmax)
ZEND_PARSE_PARAMETERS_END();

PHP_CLAMP(zvalue, zmin, zmax);
}
/* }}} */

/* {{{ Return the given value if in range of min and max */
ZEND_FRAMELESS_FUNCTION(clamp, 3)
{
zval *zvalue, *zmin, *zmax;
Z_FLF_PARAM_ZVAL(1, zvalue);
Z_FLF_PARAM_ZVAL(2, zmin);
Z_FLF_PARAM_ZVAL(3, zmax);

PHP_CLAMP(zvalue, zmin, zmax);
}
/* }}} */

/* {{{ Returns the sine of the number in radians */
PHP_FUNCTION(sin)
{
Expand Down
97 changes: 97 additions & 0 deletions ext/standard/tests/math/clamp.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
--TEST--
clamp() tests
--INI--
precision=14
date.timezone = UTC
--FILE--
<?php

var_dump(clamp(2, 1, 3));
var_dump(clamp(0, 1, 3));
var_dump(clamp(6, 1, 3));
var_dump(clamp(2, 1.3, 3.4));
var_dump(clamp(2.5, 1, 3));
var_dump(clamp(2.5, 1.3, 3.4));
var_dump(clamp(0, 1.3, 3.4));
var_dump(clamp(M_PI, -INF, INF));
var_dump(clamp(NAN, 4, 6));
var_dump(clamp("a", "c", "g"));
var_dump(clamp("d", "c", "g"));
echo clamp('2025-08-01', '2025-08-15', '2025-09-15'), "\n";
echo clamp('2025-08-20', '2025-08-15', '2025-09-15'), "\n";
echo clamp(new \DateTimeImmutable('2025-08-01'), new \DateTimeImmutable('2025-08-15'), new \DateTimeImmutable('2025-09-15'))->format('Y-m-d'), "\n";
echo clamp(new \DateTimeImmutable('2025-08-20'), new \DateTimeImmutable('2025-08-15'), new \DateTimeImmutable('2025-09-15'))->format('Y-m-d'), "\n";
var_dump(clamp(null, -1, 1));
var_dump(clamp(null, 1, 3));
var_dump(clamp(null, -3, -1));
var_dump(clamp(-9999, null, 10));
var_dump(clamp(12, null, 10));

$a = new \InvalidArgumentException('a');
$b = new \RuntimeException('b');
$c = new \LogicException('c');
echo clamp($a, $b, $c)::class, "\n";
echo clamp($b, $a, $c)::class, "\n";
echo clamp($c, $a, $b)::class, "\n";

try {
var_dump(clamp(4, NAN, 6));
} catch (ValueError $error) {
echo $error->getMessage(), "\n";
}

try {
var_dump(clamp(7, 6, NAN));
} catch (ValueError $error) {
echo $error->getMessage(), "\n";
}

try {
var_dump(clamp(1, 3, 2));
} catch (ValueError $error) {
echo $error->getMessage(), "\n";
}


try {
var_dump(clamp(-9999, 5, null));
} catch (ValueError $error) {
echo $error->getMessage(), "\n";
}

try {
var_dump(clamp(12, -5, null));
} catch (ValueError $error) {
echo $error->getMessage(), "\n";
}

?>
--EXPECT--
int(2)
int(1)
int(3)
int(2)
float(2.5)
float(2.5)
float(1.3)
float(3.141592653589793)
float(NAN)
string(1) "c"
string(1) "d"
2025-08-15
2025-08-20
2025-08-15
2025-08-20
int(-1)
int(1)
int(-3)
int(-9999)
int(10)
InvalidArgumentException
RuntimeException
LogicException
clamp(): Argument #2 ($min) cannot be NAN
clamp(): Argument #3 ($max) cannot be NAN
clamp(): Argument #2 ($min) must be smaller than or equal to argument #3 ($max)
clamp(): Argument #2 ($min) must be smaller than or equal to argument #3 ($max)
clamp(): Argument #2 ($min) must be smaller than or equal to argument #3 ($max)