-
Notifications
You must be signed in to change notification settings - Fork 866
Expand file tree
/
Copy pathclamp.xml
More file actions
151 lines (146 loc) · 4.65 KB
/
clamp.xml
File metadata and controls
151 lines (146 loc) · 4.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
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
-->