1
0
mirror of https://github.com/php/doc-zh.git synced 2026-03-26 08:02:16 +01:00
Files
2025-06-27 12:04:16 +08:00

189 lines
5.8 KiB
XML

<?xml version="1.0" encoding="utf-8"?>
<!-- $Revision$ -->
<!-- EN-Revision: 761d72245071f89a626903c9bcdc6aaff1252d54 Maintainer: dallas Status: ready -->
<!-- CREDITS: mowangjuanzi, Luffy -->
<refentry xml:id="function.max" xmlns="http://docbook.org/ns/docbook">
<refnamediv>
<refname>max</refname>
<refpurpose>找出最大值</refpurpose>
</refnamediv>
<refsect1 role="description">
&reftitle.description;
<methodsynopsis>
<type>mixed</type><methodname>max</methodname>
<methodparam><type>mixed</type><parameter>value</parameter></methodparam>
<methodparam rep="repeat"><type>mixed</type><parameter>values</parameter></methodparam>
</methodsynopsis>
<simpara>替代签名(不支持命名参数):</simpara>
<methodsynopsis>
<type>mixed</type><methodname>max</methodname>
<methodparam><type>array</type><parameter>value_array</parameter></methodparam>
</methodsynopsis>
<para>
如果仅有一个参数且为数组,<function>max</function>
返回该数组中最大的值。如果第一个参数是整数、字符串或浮点数,则至少需要两个参数而
<function>max</function> 会返回这些值中最大的一个。可以比较无限多个值。
</para>
<note>
<para>
不同类型的值将使用<link linkend="language.operators.comparison">标准比较规则</link>进行比较。例如,一个非数字
<type>string</type><type>int</type> 比较时就当做是 <literal>0</literal>,但多个非数字
<type>string</type> 值将会按照字母数字比较。返回的实际值是未应用任何转换的原始类型。
</para>
</note>
<caution>
<simpara>
传递不同类型的参数时要小心,因为 <function>max</function> 会产生不可预测的结果。
</simpara>
</caution>
</refsect1>
<refsect1 role="parameters">
&reftitle.parameters;
<para>
<variablelist>
<varlistentry>
<term><parameter>value</parameter></term>
<listitem>
<para>
任何<link linkend="language.operators.comparison">可比较</link>的值。
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><parameter>values</parameter></term>
<listitem>
<para>
任何<link linkend="language.operators.comparison">可比较</link>的值。
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><parameter>value_array</parameter></term>
<listitem>
<para>
包含值的数组。
</para>
</listitem>
</varlistentry>
</variablelist>
</para>
</refsect1>
<refsect1 role="returnvalues">
&reftitle.returnvalues;
<para>
<function>max</function> 根据标准比较返回认为是“最大”的参数值。如果不同类型的多个值认为相等(比如
<literal>0</literal><literal>'abc'</literal>),则将会返回提供给函数的第一个值。
</para>
</refsect1>
<refsect1 role="errors">
&reftitle.errors;
<para>
如果传递空数组,<function>max</function> 抛出 <classname>ValueError</classname>
</para>
</refsect1>
<refsect1 role="changelog">
&reftitle.changelog;
<informaltable>
<tgroup cols="2">
<thead>
<row>
<entry>&Version;</entry>
<entry>&Description;</entry>
</row>
</thead>
<tbody>
<row>
<entry>8.0.0</entry>
<entry>
<function>max</function> 现在失败时会抛出 <classname>ValueError</classname>;之前会返回
&false; 并发出 <constant>E_WARNING</constant> 错误。
</entry>
</row>
<row>
<entry>8.0.0</entry>
<entry>
由于 <link linkend="migration80.incompatible.core.string-number-comparision">字符串到数字的比较</link>
已经改变,<function>max</function> 在这些情况下不再根据参数的顺序返回不同的值。
</entry>
</row>
</tbody>
</tgroup>
</informaltable>
</refsect1>
<refsect1 role="examples">
&reftitle.examples;
<para>
<example>
<title>使用 <function>max</function> 的示例</title>
<programlisting role="php">
<![CDATA[
<?php
echo max(2, 3, 1, 6, 7), PHP_EOL; // 7
echo max(array(2, 4, 5)), PHP_EOL; // 5
// Here we are comparing -1 < 0, so 'hello' is the highest value
echo max('hello', -1), PHP_EOL; // hello
// With multiple arrays of different lengths, max returns the longest
$val = max(array(2, 2, 2), array(1, 1, 1, 1)); // array(1, 1, 1, 1)
var_dump($val);
// Multiple arrays of the same length are compared from left to right
// so in our example: 2 == 2, but 5 > 4
$val = max(array(2, 4, 8), array(2, 5, 1)); // array(2, 5, 1)
var_dump($val);
// 如果同时给出数组和非数组,则绝对不会返回数组
// 因为比较认为数组大于任何值
$val = max('string', array(2, 5, 7), 42); // array(2, 5, 7)
var_dump($val);
// If one argument is NULL or a boolean, it will be compared against
// other values using the rule FALSE < TRUE regardless of the other types involved
// In the below example, -10 is treated as TRUE in the comparison
$val = max(-10, FALSE); // -10
var_dump($val);
// 0, on the other hand, is treated as FALSE, so is "lower than" TRUE
$val = max(0, TRUE); // TRUE
var_dump($val);
?>
]]>
</programlisting>
</example>
</para>
</refsect1>
<refsect1 role="seealso">
&reftitle.seealso;
<para>
<simplelist>
<member><function>min</function></member>
<member><function>count</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
-->