1
0
mirror of https://github.com/php/doc-ja.git synced 2026-04-24 08:28:13 +02:00
Files
archived-doc-ja/language/control-structures/match.xml
T
2025-08-28 17:44:51 +09:00

329 lines
7.9 KiB
XML

<?xml version="1.0" encoding="utf-8"?>
<!-- $Revision$ -->
<!-- EN-Revision: 17502ebe0691a84e7d0738c13e8c1061dde98de7 Maintainer: mumumu Status: ready -->
<sect1 xml:id="control-structures.match" xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink">
<title>match</title>
<?phpdoc print-version-for="match"?>
<para>
<literal>match</literal> 式は、値の一致をチェックした結果に基づいて評価結果を分岐します。
<literal>switch</literal> 文と似ていますが、
<literal>match</literal> 式は複数の候補と比較される制約式を持ちます。
<literal>switch</literal> 文とは異なり、
三項演算子のように値を評価します。
<literal>switch</literal> 文とは異なり、
弱い比較(<code>==</code>)ではなく、
型と値の一致チェック(<code>===</code>) に基づいて行われます。
match 式は PHP 8.0.0 以降で利用可能です。
</para>
<example>
<title><literal>match</literal> 式の構造</title>
<programlisting role="php">
<![CDATA[
<?php
$return_value = match (制約式) {
単一の条件式 => 返却式,
条件式1, 条件式2 => 返却式,
};
?>
]]>
</programlisting>
<example>
<title>基本式な <literal>match</literal> 式の使い方</title>
<programlisting role="php">
<![CDATA[
<?php
$food = 'cake';
$return_value = match ($food) {
'apple' => 'This food is an apple',
'bar' => 'This food is a bar',
'cake' => 'This food is a cake',
};
var_dump($return_value);
?>
]]>
</programlisting>
&example.outputs;
<screen>
<![CDATA[
string(19) "This food is a cake"
]]>
</screen>
</example>
<example>
<title>比較演算子と <literal>match</literal> 式を一緒に使う例</title>
<programlisting role="php">
<![CDATA[
<?php
$age = 18;
$output = match (true) {
$age < 2 => "Baby",
$age < 13 => "Child",
$age <= 19 => "Teenager",
$age >= 40 => "Old adult",
$age > 19 => "Young adult",
};
var_dump($output);
?>
]]>
</programlisting>
&example.outputs;
<screen>
<![CDATA[
string(8) "Teenager"
]]>
</screen>
</example>
<note>
<simpara>
<literal>match</literal> 式の結果は、必ずしも使う必要はありません。
</simpara>
</note>
<note>
<simpara>
<literal>match</literal> 式を単一の式として使う場合は、必ずセミコロン <literal>;</literal> で終わらなければなりません。
</simpara>
</note>
</example>
<para>
<literal>match</literal> 式は、
<literal>switch</literal> 文と似ていますが、いくつかの違いがあります:
<itemizedlist>
<listitem>
<simpara>
<literal>match</literal> 式の比較は、
switch 文が行う弱い比較ではなく、
厳密に値を比較(<code>===</code>) します。
</simpara>
</listitem>
<listitem>
<simpara>
<literal>match</literal> 式は値を返します。
</simpara>
</listitem>
<listitem>
<simpara>
<literal>match</literal> 式の分岐は、
<literal>switch</literal> 文のように後の分岐に抜けたりはしません。
</simpara>
</listitem>
<listitem>
<simpara>
<literal>match</literal> 式は、全ての場合を網羅していなければいけません。
</simpara>
</listitem>
</itemizedlist>
</para>
<para>
<literal>switch</literal> 文のように、
<literal>match</literal> 式はマッチさせる分岐をひとつひとつ実行します。
はじめは、コードは何も実行されません。
以前のすべての条件式が、制約式とマッチしなかった場合に条件式が実行されます。
条件式に一致する式が評価された場合に、返却式が評価されます。
たとえば、以下のようになります:
<informalexample>
<programlisting role="php">
<![CDATA[
<?php
$result = match ($x) {
foo() => 'value',
$this->bar() => 'value', // foo() === $x であれば $this->bar() は呼び出されません
$this->baz => beep(), // $x === $this->baz でなければ beep() は呼び出されません。
// などなど
};
?>
]]>
</programlisting>
</informalexample>
</para>
<para>
<literal>match</literal> 式の分岐は、複数の式をカンマ区切りで含めても構いません。
これは論理ORであり、複数の分岐の右辺を同じにする場合の短縮記法です。
</para>
<para>
<informalexample>
<programlisting role="php">
<![CDATA[
<?php
$result = match ($x) {
// この分岐は:
$a, $b, $c => 5,
// 以下の3つの分岐と等しい:
$a => 5,
$b => 5,
$c => 5,
};
?>
]]>
</programlisting>
</informalexample>
</para>
<para>
<literal>default</literal> パターンという特別な場合があります。
このパターンは前の分岐にマッチしなかったあらゆる場合にマッチします。
たとえば、以下のようになります:
<informalexample>
<programlisting role="php">
<![CDATA[
<?php
$expressionResult = match ($condition) {
1, 2 => foo(),
3, 4 => bar(),
default => baz(),
};
?>
]]>
</programlisting>
</informalexample>
<note>
<simpara>
複数の <literal>default</literal> パターンがあった場合、
<constant>E_FATAL_ERROR</constant> が発生します。
</simpara>
</note>
</para>
<para>
<literal>match</literal> 式は、全ての場合を網羅していなければいけません。
制約式がどの分岐でも処理できなかった場合、
<classname>UnhandledMatchError</classname> がスローされます。
</para>
<example>
<title>処理されない match 式の例</title>
<programlisting role="php">
<![CDATA[
<?php
$condition = 5;
try {
match ($condition) {
1, 2 => foo(),
3, 4 => bar(),
};
} catch (\UnhandledMatchError $e) {
var_dump($e);
}
?>
]]>
</programlisting>
&example.outputs;
<screen>
<![CDATA[
object(UnhandledMatchError)#1 (7) {
["message":protected]=>
string(33) "Unhandled match value of type int"
["string":"Error":private]=>
string(0) ""
["code":protected]=>
int(0)
["file":protected]=>
string(9) "/in/ICgGK"
["line":protected]=>
int(6)
["trace":"Error":private]=>
array(0) {
}
["previous":"Error":private]=>
NULL
}
]]>
</screen>
</example>
<sect2>
<title>厳密な一致チェックを行わずに match 式を使う</title>
<para>
制約式に <code>true</code> を指定することで、
厳密な一致チェックを行わずに <literal>match</literal> 式を使うことができます。
</para>
<example>
<title>整数の範囲に応じてmatch式を分岐させる一般的な使い方</title>
<programlisting role="php">
<![CDATA[
<?php
$age = 23;
$result = match (true) {
$age >= 65 => 'senior',
$age >= 25 => 'adult',
$age >= 18 => 'young adult',
default => 'kid',
};
var_dump($result);
?>
]]>
</programlisting>
&example.outputs;
<screen>
<![CDATA[
string(11) "young adult"
]]>
</screen>
</example>
<example>
<title>文字列の内容に応じてmatch式を分岐させる一般的な使い方</title>
<programlisting role="php">
<![CDATA[
<?php
$text = 'Bienvenue chez nous';
$result = match (true) {
str_contains($text, 'Welcome'), str_contains($text, 'Hello') => 'en',
str_contains($text, 'Bienvenue'), str_contains($text, 'Bonjour') => 'fr',
// ...
};
var_dump($result);
?>
]]>
</programlisting>
&example.outputs;
<screen>
<![CDATA[
string(2) "fr"
]]>
</screen>
</example>
</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
-->