Skip to content

Document clamp function #4814

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
34 changes: 34 additions & 0 deletions appendices/migration85/new-functions.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?xml version="1.0" encoding="utf-8"?>
<sect1 xml:id="migration85.new-functions">
<title>New Functions</title>

<sect2 xml:id="migration85.new-functions.core">
<title>Core</title>

<simplelist>
<!-- RFC: https://wiki.php.net/rfc/clamp -->
<member><function>clamp</function></member>
</simplelist>
</sect2>

</sect1>
<!-- Keep this comment at the end of the file
Local variables:
mode: sgml
sgml-omittag:t
sgml-shorttag:t
sgml-minimize-attributes:nil
sgml-always-quote-attributes:t
sgml-indent-step:1
sgml-indent-data:t
indent-tabs-mode:nil
sgml-parent-document:nil
sgml-default-dtd-file:"~/.phpdoc/manual.ced"
sgml-exposed-tags:nil
sgml-local-catalogs:nil
sgml-local-ecat-files:nil
End:
vim600: syn=xml fen fdm=syntax fdl=2 si
vim: et tw=78 syn=sgml
vi: ts=1 sw=1
-->
1 change: 1 addition & 0 deletions reference/array/versions.xml
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
<function name='array_walk_recursive' from='PHP 5, PHP 7, PHP 8'/>
<function name='arsort' from='PHP 4, PHP 5, PHP 7, PHP 8'/>
<function name='asort' from='PHP 4, PHP 5, PHP 7, PHP 8'/>
<function name='clamp' from='PHP 8 &gt;= 8.5.0'/>
<function name='compact' from='PHP 4, PHP 5, PHP 7, PHP 8'/>
<function name='count' from='PHP 4, PHP 5, PHP 7, PHP 8'/>
<function name='current' from='PHP 4, PHP 5, PHP 7, PHP 8'/>
Expand Down
151 changes: 151 additions & 0 deletions reference/math/functions/clamp.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- $Revision$ -->
<refentry xml:id="function.clamp" xmlns="http://docbook.org/ns/docbook">
<refnamediv>
<refname>clamp</refname>
<refpurpose>Return the given value if in range, else return the nearest bound</refpurpose>
</refnamediv>
<refsect1 role="description">
&reftitle.description;
<methodsynopsis>
<type>mixed</type><methodname>clamp</methodname>
<methodparam><type>mixed</type><parameter>value</parameter></methodparam>
<methodparam><type>mixed</type><parameter>min</parameter></methodparam>
<methodparam><type>mixed</type><parameter>max</parameter></methodparam>
</methodsynopsis>
<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>
<note>
<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>
</note>
<caution>
<simpara>
Be careful when passing arguments of different types because
<function>clamp</function> can produce unpredictable results.
</simpara>
</caution>
</refsect1>
<refsect1 role="parameters">
&reftitle.parameters;
<para>
<variablelist>
<varlistentry>
<term><parameter>value</parameter></term>
<listitem>
<para>
Any <link linkend="language.operators.comparison">comparable</link>
value to be clamped between <parameter>min</parameter> and <parameter>max</parameter>.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><parameter>min</parameter></term>
<listitem>
<para>
A <link linkend="language.operators.comparison">comparable</link> minimum to limit
the <parameter>value</parameter> to.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><parameter>max</parameter></term>
<listitem>
<para>
A <link linkend="language.operators.comparison">comparable</link> maximum to limit
the <parameter>value</parameter> to.
</para>
</listitem>
</varlistentry>
</variablelist>
</para>
</refsect1>
<refsect1 role="returnvalues">
&reftitle.returnvalues;
<para>
<function>clamp</function> returns the parameter <parameter>value</parameter> if
considered "between" <parameter>min</parameter> and <parameter>max</parameter> according to standard
comparisons.
</para>
<para>
If <parameter>value</parameter> is <constant>NAN</constant>, then the returned value will also be
<constant>NAN</constant>.
</para>
</refsect1>

<refsect1 role="errors">
&reftitle.errors;
<para>
If <parameter>min</parameter> is greater than <parameter>max</parameter>,
<function>clamp</function> throws a <classname>ValueError</classname>.
</para>
<para>
If <parameter>min</parameter> or <parameter>max</parameter> is <constant>NAN</constant>,
<function>clamp</function> throws a <classname>ValueError</classname>.
</para>
</refsect1>

<refsect1 role="examples">
&reftitle.examples;
<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>

</refsect1>
<refsect1 role="seealso">
&reftitle.seealso;
<para>
<simplelist>
<member><function>min</function></member>
<member><function>max</function></member>
</simplelist>
</para>
</refsect1>
</refentry>
<!-- Keep this comment at the end of the file
Local variables:
mode: sgml
sgml-omittag:t
sgml-shorttag:t
sgml-minimize-attributes:nil
sgml-always-quote-attributes:t
sgml-indent-step:1
sgml-indent-data:t
indent-tabs-mode:nil
sgml-parent-document:nil
sgml-default-dtd-file:"~/.phpdoc/manual.ced"
sgml-exposed-tags:nil
sgml-local-catalogs:nil
sgml-local-ecat-files:nil
End:
vim600: syn=xml fen fdm=syntax fdl=2 si
vim: et tw=78 syn=sgml
vi: ts=1 sw=1
-->