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
34 changes: 34 additions & 0 deletions appendices/migration86/new-functions.xml
Original file line number Diff line number Diff line change
@@ -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.

<sect1 xml:id="migration86.new-functions">
<title>New Functions</title>

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

<simplelist>
<!-- RFC: https://wiki.php.net/rfc/clamp_v2 -->
<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 @@ -66,6 +66,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.6.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>
Comment on lines +16 to +20
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>

<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>
Comment on lines +22 to +28
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>

</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>
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?

<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>
Comment on lines +44 to +47
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>

</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>
Comment on lines +53 to +56
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>
A <link linkend="language.operators.comparison">comparable</link> minimum to limit
the <parameter>value</parameter> to.
</para>
<simpara>
A <link linkend="language.operators.comparison">comparable</link> minimum to limit
the <parameter>value</parameter> to.
</simpara>

</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>
Comment on lines +62 to +65
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>
A <link linkend="language.operators.comparison">comparable</link> maximum to limit
the <parameter>value</parameter> to.
</para>
<simpara>
A <link linkend="language.operators.comparison">comparable</link> maximum to limit
the <parameter>value</parameter> to.
</simpara>

</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>
Comment on lines +73 to +77
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>
<function>clamp</function> returns the parameter <parameter>value</parameter> if
considered "between" <parameter>min</parameter> and <parameter>max</parameter> according to standard
comparisons.
</para>
<simpara>
<function>clamp</function> returns the parameter <parameter>value</parameter> if
considered "between" <parameter>min</parameter> and <parameter>max</parameter> according to standard
comparisons.
</simpara>

<para>
If <parameter>value</parameter> is <constant>NAN</constant>, then the returned value will also be
<constant>NAN</constant>.
</para>
Comment on lines +78 to +81
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>

</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>
Comment on lines +86 to +89
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>

<para>
If <parameter>min</parameter> or <parameter>max</parameter> is <constant>NAN</constant>,
<function>clamp</function> throws a <classname>ValueError</classname>.
</para>
Comment on lines +90 to +93
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>

</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>
Comment on lines +98 to +119
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>?

<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
-->