Skip to content

Document clamp function#4814

Open
kylekatarnls wants to merge 1 commit into
php:masterfrom
kylekatarnls:clamp-function
Open

Document clamp function#4814
kylekatarnls wants to merge 1 commit into
php:masterfrom
kylekatarnls:clamp-function

Conversation

@kylekatarnls
Copy link
Copy Markdown
Contributor

@kylekatarnls kylekatarnls marked this pull request as ready for review December 7, 2025 15:23
@lacatoire
Copy link
Copy Markdown
Member

LGTM, clear, consistent with the RFC, the implementation, and the tests.

Thanks for adding this @kylekatarnls

nicolas-grekas added a commit to symfony/polyfill that referenced this pull request Apr 10, 2026
This PR was merged into the 1.x branch.

Discussion
----------

[8.6] Add `clamp` function

RFC: https://wiki.php.net/rfc/clamp_v2
Original implementation: php/php-src#19434
Documentation: php/doc-en#4814

Commits
-------

2b4c47d [8.6] Add `clamp` function
Copy link
Copy Markdown
Member

@jordikroon jordikroon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We cannot merge this yet as 8.6 isn't released yet.
Mostly doc style comments.

Since date comparison is a big part of this RFC. We should document this explicitly here as well. Not only within the examples section.

@@ -0,0 +1,34 @@
<?xml version="1.0" encoding="utf-8"?>
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please don't add file this (yet). While I appreciate your thought, we will have to create migration guides anyway. This would ultimately create a merge conflict when the time is there. And then it would be more easy to merge this.

Comment on lines +16 to +20
<para>
Return the given value if in range of <parameter>min</parameter> and <parameter>max</parameter>.
Else if it's lower than <parameter>min</parameter>, return <parameter>min</parameter>.
Else return <parameter>max</parameter>.
</para>
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
<para>
Return the given value if in range of <parameter>min</parameter> and <parameter>max</parameter>.
Else if it's lower than <parameter>min</parameter>, return <parameter>min</parameter>.
Else return <parameter>max</parameter>.
</para>
<simpara>
Return the given value if in range of <parameter>min</parameter> and <parameter>max</parameter>.
Else if it's lower than <parameter>min</parameter>, return <parameter>min</parameter>.
Else return <parameter>max</parameter>.
</simpara>

Comment on lines +22 to +28
<para>
Values of different types will be compared using the <link linkend="language.operators.comparison">
standard comparison rules</link>. For instance, a non-numeric <type>string</type> will be
compared to an <type>int</type> as though it were <literal>0</literal>, but multiple non-numeric
<type>string</type> values will be compared alphanumerically. The actual value returned will be of the
original type with no conversion applied.
</para>
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
<para>
Values of different types will be compared using the <link linkend="language.operators.comparison">
standard comparison rules</link>. For instance, a non-numeric <type>string</type> will be
compared to an <type>int</type> as though it were <literal>0</literal>, but multiple non-numeric
<type>string</type> values will be compared alphanumerically. The actual value returned will be of the
original type with no conversion applied.
</para>
<simpara>
Values of different types will be compared using the <link linkend="language.operators.comparison">
standard comparison rules</link>. For instance, a non-numeric <type>string</type> will be
compared to an <type>int</type> as though it were <literal>0</literal>, but multiple non-numeric
<type>string</type> values will be compared alphanumerically. The actual value returned will be of the
original type with no conversion applied.
</simpara>

</refsect1>
<refsect1 role="parameters">
&reftitle.parameters;
<para>
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

<variablelist> doesn't need to be wrapped in <para>. Could you remove this container?

Comment on lines +44 to +47
<para>
Any <link linkend="language.operators.comparison">comparable</link>
value to be clamped between <parameter>min</parameter> and <parameter>max</parameter>.
</para>
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
<para>
Any <link linkend="language.operators.comparison">comparable</link>
value to be clamped between <parameter>min</parameter> and <parameter>max</parameter>.
</para>
<simpara>
Any <link linkend="language.operators.comparison">comparable</link>
value to be clamped between <parameter>min</parameter> and <parameter>max</parameter>.
</simpara>

Comment on lines +78 to +81
<para>
If <parameter>value</parameter> is <constant>NAN</constant>, then the returned value will also be
<constant>NAN</constant>.
</para>
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
<para>
If <parameter>value</parameter> is <constant>NAN</constant>, then the returned value will also be
<constant>NAN</constant>.
</para>
<simpara>
If <parameter>value</parameter> is <constant>NAN</constant>, then the returned value will also be
<constant>NAN</constant>.
</simpara>

Comment on lines +86 to +89
<para>
If <parameter>min</parameter> is greater than <parameter>max</parameter>,
<function>clamp</function> throws a <classname>ValueError</classname>.
</para>
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
<para>
If <parameter>min</parameter> is greater than <parameter>max</parameter>,
<function>clamp</function> throws a <classname>ValueError</classname>.
</para>
<simpara>
If <parameter>min</parameter> is greater than <parameter>max</parameter>,
<function>clamp</function> throws a <exceptionname>ValueError</exceptionname>.
</simpara>

Comment on lines +90 to +93
<para>
If <parameter>min</parameter> or <parameter>max</parameter> is <constant>NAN</constant>,
<function>clamp</function> throws a <classname>ValueError</classname>.
</para>
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
<para>
If <parameter>min</parameter> or <parameter>max</parameter> is <constant>NAN</constant>,
<function>clamp</function> throws a <classname>ValueError</classname>.
</para>
<simpara>
If <parameter>min</parameter> or <parameter>max</parameter> is <constant>NAN</constant>,
<function>clamp</function> throws a <exceptionname>ValueError</exceptionname>.
</simpara>

Comment on lines +98 to +119
<para>
<example>
<title>Example uses of <function>clamp</function></title>
<programlisting role="php">
<![CDATA[
<?php
echo clamp(-5, min: 0, max: 100), PHP_EOL; // 0
echo clamp(55, min: 0, max: 100), PHP_EOL; // 55
echo clamp(103, min: 0, max: 100), PHP_EOL; // 100

echo clamp("J", min: "A", max: "F"), PHP_EOL; // "F"

clamp(
new \DateTimeImmutable('2025-08-01'),
min: new \DateTimeImmutable('2025-08-15'),
max: new \DateTimeImmutable('2025-09-15'),
)->format('Y-m-d'), PHP_EOL; // 2025-08-15
?>
]]>
</programlisting>
</example>
</para>
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
<para>
<example>
<title>Example uses of <function>clamp</function></title>
<programlisting role="php">
<![CDATA[
<?php
echo clamp(-5, min: 0, max: 100), PHP_EOL; // 0
echo clamp(55, min: 0, max: 100), PHP_EOL; // 55
echo clamp(103, min: 0, max: 100), PHP_EOL; // 100
echo clamp("J", min: "A", max: "F"), PHP_EOL; // "F"
clamp(
new \DateTimeImmutable('2025-08-01'),
min: new \DateTimeImmutable('2025-08-15'),
max: new \DateTimeImmutable('2025-09-15'),
)->format('Y-m-d'), PHP_EOL; // 2025-08-15
?>
]]>
</programlisting>
</example>
</para>
<informalexample>
<programlisting role="php">
<![CDATA[
<?php
echo clamp(-5, min: 0, max: 100), PHP_EOL; // 0
echo clamp(55, min: 0, max: 100), PHP_EOL; // 55
echo clamp(103, min: 0, max: 100), PHP_EOL; // 100
echo clamp("J", min: "A", max: "F"), PHP_EOL; // "F"
clamp(
new \DateTimeImmutable('2025-08-01'),
min: new \DateTimeImmutable('2025-08-15'),
max: new \DateTimeImmutable('2025-09-15'),
)->format('Y-m-d'), PHP_EOL; // 2025-08-15
?>
]]>
</programlisting>
</informalexample>

</refsect1>
<refsect1 role="seealso">
&reftitle.seealso;
<para>
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you remove this outer <para>?

@jordikroon jordikroon added this to the PHP 8.6 milestone May 18, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants