<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$TOC_DEPRECATED = array();
$PARENTS = array();
include_once dirname(__FILE__) ."/toc/language.control-structures.inc";
$setup = array (
  'home' => 
  array (
    0 => 'index.php',
    1 => 'PHP Manual',
  ),
  'head' => 
  array (
    0 => 'UTF-8',
    1 => 'de',
  ),
  'this' => 
  array (
    0 => 'control-structures.match.php',
    1 => 'match',
    2 => 'match',
  ),
  'up' => 
  array (
    0 => 'language.control-structures.php',
    1 => 'Kontrollstrukturen',
  ),
  'prev' => 
  array (
    0 => 'control-structures.switch.php',
    1 => 'switch',
  ),
  'next' => 
  array (
    0 => 'control-structures.declare.php',
    1 => 'declare',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'de',
    'path' => 'language/control-structures/match.xml',
  ),
  'history' => 
  array (
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="control-structures.match" class="sect1">
 <h2 class="title">match</h2>
 <p class="verinfo">(PHP 8)</p>
 <p class="para">
  Der <code class="literal">match</code>-Ausdruck verzweigt die Auswertung auf
  der Grundlage einer Identitätsprüfung eines Wertes. Ähnlich wie eine
  <code class="literal">switch</code>-Anweisung, hat ein
  <code class="literal">match</code>-Ausdruck ein Subjekt, welcher mit
  mehreren Fällen verglichen wird. Im Gegensatz zu switch ist der
  Vergleich typsicher (<code class="code">===</code>) und nicht typschwache (<code class="code">==</code>).
  Match-Ausdrücke sind ab PHP 8.0.0 verfügbar.
 </p>

 <div class="example" id="example-1">
  <p><strong>Beispiel #1 Struktur eines <code class="literal">match</code>-Ausdrucks</strong></p>
  <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />$return_value </span><span style="color: #007700">= match (</span><span style="color: #0000BB">subjekt</span><span style="color: #007700">) {<br />    </span><span style="color: #0000BB">einfacher_bedingter_ausdruck </span><span style="color: #007700">=&gt; </span><span style="color: #0000BB">rückgabe_ausdruck</span><span style="color: #007700">,<br />    </span><span style="color: #0000BB">bedingter_ausdruck1</span><span style="color: #007700">, </span><span style="color: #0000BB">bedingter_ausdruck2 </span><span style="color: #007700">=&gt; </span><span style="color: #0000BB">rückgabe_ausdruck</span><span style="color: #007700">,<br />};<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
  </div>


  <div class="example" id="example-2">
   <p><strong>Beispiel #2 Grundlegende Benutzung von <code class="literal">match</code></strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />$lebensmittel </span><span style="color: #007700">= </span><span style="color: #DD0000">'kuchen'</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">$return_value </span><span style="color: #007700">= match (</span><span style="color: #0000BB">$lebensmittel</span><span style="color: #007700">) {<br />    </span><span style="color: #DD0000">'apfel' </span><span style="color: #007700">=&gt; </span><span style="color: #DD0000">'Das Lebensmittel ist ein Apfel'</span><span style="color: #007700">,<br />    </span><span style="color: #DD0000">'schokolade' </span><span style="color: #007700">=&gt; </span><span style="color: #DD0000">'Das Lebensmittel ist Schokolade'</span><span style="color: #007700">,<br />    </span><span style="color: #DD0000">'kuchen' </span><span style="color: #007700">=&gt; </span><span style="color: #DD0000">'Das Lebensmittel ist ein Kuchen'</span><span style="color: #007700">,<br />};<br /><br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$return_value</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
  </div>

  <div class="example-contents"><p>Das oben gezeigte Beispiel erzeugt folgende Ausgabe:</p></div>
   <div class="example-contents screen">
<div class="cdata"><pre>
string(31) &quot;Das Lebensmittel ist ein Kuchen&quot;
</pre></div>
   </div>
  </div>

  <div class="example" id="example-3">
   <p><strong>Beispiel #3 Beispiel für die Benutzung von <code class="literal">match</code> mit Vergleichsoperatoren</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />$age </span><span style="color: #007700">= </span><span style="color: #0000BB">18</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">$output </span><span style="color: #007700">= match (</span><span style="color: #0000BB">true</span><span style="color: #007700">) {<br />    </span><span style="color: #0000BB">$age </span><span style="color: #007700">&lt; </span><span style="color: #0000BB">2 </span><span style="color: #007700">=&gt; </span><span style="color: #DD0000">"Baby"</span><span style="color: #007700">,<br />    </span><span style="color: #0000BB">$age </span><span style="color: #007700">&lt; </span><span style="color: #0000BB">13 </span><span style="color: #007700">=&gt; </span><span style="color: #DD0000">"Kind"</span><span style="color: #007700">,<br />    </span><span style="color: #0000BB">$age </span><span style="color: #007700">&lt;= </span><span style="color: #0000BB">19 </span><span style="color: #007700">=&gt; </span><span style="color: #DD0000">"Teenager"</span><span style="color: #007700">,<br />    </span><span style="color: #0000BB">$age </span><span style="color: #007700">&gt;= </span><span style="color: #0000BB">40 </span><span style="color: #007700">=&gt; </span><span style="color: #DD0000">"Alter Erwachsener"</span><span style="color: #007700">,<br />    </span><span style="color: #0000BB">$age </span><span style="color: #007700">&gt; </span><span style="color: #0000BB">19 </span><span style="color: #007700">=&gt; </span><span style="color: #DD0000">"Junger Erwachsener"</span><span style="color: #007700">,<br />};<br /><br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$output</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
  </div>

  <div class="example-contents"><p>Das oben gezeigte Beispiel erzeugt folgende Ausgabe:</p></div>
   <div class="example-contents screen">
<div class="cdata"><pre>
string(8) &quot;Teenager&quot;
</pre></div>
   </div>
  </div>

  <blockquote class="note"><p><strong class="note">Hinweis</strong>: 
   <span class="simpara">
    Das Ergebnis eines <code class="literal">match</code>-Ausdrucks muss nicht verwendet werden.
   </span>
  </p></blockquote>
  <blockquote class="note"><p><strong class="note">Hinweis</strong>: 
   <span class="simpara">
    Wenn ein <code class="literal">match</code>-Ausdruck als eigenständiger Ausdruck
    verwendet wird, <em>muss</em> er mit einem Semikolon
    <code class="literal">;</code> abgeschlossen werden.
   </span>
  </p></blockquote>
 </div>

 <p class="para">
  Der Ausdruck <code class="literal">match</code> ist ähnlich wie eine
  <code class="literal">switch</code>-Anweisung, hat aber einige wesentliche Unterschiede:

  <ul class="itemizedlist">
   <li class="listitem">
    <span class="simpara">
     Ein <code class="literal">match</code>-Fall vergleicht Werte typsicher (<code class="code">===</code>)
     statt typschwach, wie die switch-Anweisung.
    </span>
   </li>
   <li class="listitem">
    <span class="simpara">
     Ein <code class="literal">match</code>-Ausdruck gibt einen Wert zurück.
    </span>
   </li>
   <li class="listitem">
    <span class="simpara">
     <code class="literal">match</code>-Fälle fallen nicht auf spätere Fälle durch, so wie
     es <code class="literal">switch</code>-Fälle tun können.
    </span>
   </li>
   <li class="listitem">
    <span class="simpara">
     Ein <code class="literal">match</code>-Ausdruck muss vollständig sein.
    </span>
   </li>
  </ul>
 </p>

 <p class="para">
  Wie <code class="literal">switch</code>-Anweisungen, werden
  <code class="literal">match</code>-Ausdrücke Fall für Fall ausgeführt.
  Zu Beginn wird kein Code ausgeführt.
  Die bedingten Ausdrücke werden nur ausgewertet, wenn alle vorherigen bedingten
  Ausdrücke nicht mit dem betreffenden Ausdruck übereinstimmen.
  Nur der Rückgabeausdruck, der dem passenden bedingten
  Ausdruck entspricht, wird ausgewertet.
  Ein Beispiel dafür:
  <div class="informalexample">
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />$result </span><span style="color: #007700">= match (</span><span style="color: #0000BB">$x</span><span style="color: #007700">) {<br />    </span><span style="color: #0000BB">foo</span><span style="color: #007700">() =&gt; </span><span style="color: #DD0000">'value'</span><span style="color: #007700">,<br />    </span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">bar</span><span style="color: #007700">() =&gt; </span><span style="color: #DD0000">'value'</span><span style="color: #007700">, </span><span style="color: #FF8000">// $this-&gt;bar() wird nicht aufgerufen, wenn foo() === $x<br />    </span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">baz </span><span style="color: #007700">=&gt; </span><span style="color: #0000BB">beep</span><span style="color: #007700">(), </span><span style="color: #FF8000">// beep() wird nur aufgerufen, wenn $x === $this-&gt;baz<br />    // etc.<br /></span><span style="color: #007700">};<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
   </div>

  </div>
 </p>

 <p class="para">
  <code class="literal">match</code>-Bedingungen können mehrere Ausdrücke, die durch Kommata
  getrennt sind, enthalten. Diese Ausdrücke werden durch ein logisches ODER
  getrennt und sind ein Kürzel für mehrere <code class="literal">match</code>-Fälle,
  welche die gleiche rechte Seite haben.
 </p>
 <p class="para">
  <div class="informalexample">
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />$result </span><span style="color: #007700">= match (</span><span style="color: #0000BB">$x</span><span style="color: #007700">) {<br />    </span><span style="color: #FF8000">// Dieser Fall:<br />    </span><span style="color: #0000BB">$a</span><span style="color: #007700">, </span><span style="color: #0000BB">$b</span><span style="color: #007700">, </span><span style="color: #0000BB">$c </span><span style="color: #007700">=&gt; </span><span style="color: #0000BB">5</span><span style="color: #007700">,<br />    </span><span style="color: #FF8000">// ist der gleiche Fall wie:<br />    </span><span style="color: #0000BB">$a </span><span style="color: #007700">=&gt; </span><span style="color: #0000BB">5</span><span style="color: #007700">,<br />    </span><span style="color: #0000BB">$b </span><span style="color: #007700">=&gt; </span><span style="color: #0000BB">5</span><span style="color: #007700">,<br />    </span><span style="color: #0000BB">$c </span><span style="color: #007700">=&gt; </span><span style="color: #0000BB">5</span><span style="color: #007700">,<br />};<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
   </div>

  </div>
 </p>
 <p class="para">
  Ein Sonderfall ist die <code class="literal">default</code>-Klausel.
  Diese fängt alle Fälle ab, die nicht durch einen der anderen Fälle behandelt wurden.
  Zum Beispiel:
  <div class="informalexample">
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />$expressionResult </span><span style="color: #007700">= match (</span><span style="color: #0000BB">$condition</span><span style="color: #007700">) {<br />    </span><span style="color: #0000BB">1</span><span style="color: #007700">, </span><span style="color: #0000BB">2 </span><span style="color: #007700">=&gt; </span><span style="color: #0000BB">foo</span><span style="color: #007700">(),<br />    </span><span style="color: #0000BB">3</span><span style="color: #007700">, </span><span style="color: #0000BB">4 </span><span style="color: #007700">=&gt; </span><span style="color: #0000BB">bar</span><span style="color: #007700">(),<br />    default =&gt; </span><span style="color: #0000BB">baz</span><span style="color: #007700">(),<br />};<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
   </div>

  </div>
  <blockquote class="note"><p><strong class="note">Hinweis</strong>: 
   <span class="simpara">
    Mehrere default-Anweisungen erzeugen einen
    <strong><code><a href="errorfunc.constants.php#constant.e-compile-error">E_COMPILE_ERROR</a></code></strong>-Fehler.
   </span>
  </p></blockquote>
 </p>

 <p class="para">
  Ein <code class="literal">match</code>-Ausdruck muss vollständig sein. Wenn
  die Bedingung von keinem Fall behandelt wird, wird eine
  <span class="classname"><a href="class.unhandledmatcherror.php" class="classname">UnhandledMatchError</a></span>-Exception geworfen.
 </p>

 <div class="example" id="example-4">
  <p><strong>Beispiel #4 Beispiel für einen unbehandelten Vergleichsausdruck</strong></p>
  <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />$bedingung </span><span style="color: #007700">= </span><span style="color: #0000BB">5</span><span style="color: #007700">;<br /><br />try {<br />    match (</span><span style="color: #0000BB">$bedingung</span><span style="color: #007700">) {<br />        </span><span style="color: #0000BB">1</span><span style="color: #007700">, </span><span style="color: #0000BB">2 </span><span style="color: #007700">=&gt; </span><span style="color: #0000BB">foo</span><span style="color: #007700">(),<br />        </span><span style="color: #0000BB">3</span><span style="color: #007700">, </span><span style="color: #0000BB">4 </span><span style="color: #007700">=&gt; </span><span style="color: #0000BB">bar</span><span style="color: #007700">(),<br />    };<br />} catch (</span><span style="color: #0000BB">\UnhandledMatchError $e</span><span style="color: #007700">) {<br />    </span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$e</span><span style="color: #007700">);<br />}<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
  </div>

  <div class="example-contents"><p>Das oben gezeigte Beispiel erzeugt folgende Ausgabe:</p></div>
  <div class="example-contents screen">
<div class="cdata"><pre>
object(UnhandledMatchError)#1 (7) {
  [&quot;message&quot;:protected]=&gt;
  string(33) &quot;Unhandled match value of type int&quot;
  [&quot;string&quot;:&quot;Error&quot;:private]=&gt;
  string(0) &quot;&quot;
  [&quot;code&quot;:protected]=&gt;
  int(0)
  [&quot;file&quot;:protected]=&gt;
  string(9) &quot;/in/ICgGK&quot;
  [&quot;line&quot;:protected]=&gt;
  int(6)
  [&quot;trace&quot;:&quot;Error&quot;:private]=&gt;
  array(0) {
  }
  [&quot;previous&quot;:&quot;Error&quot;:private]=&gt;
  NULL
}
</pre></div>
  </div>
 </div>

 <div class="sect2">
  <h3 class="title">Verwendung von Ausdrücken zur Handhabung von Nicht-Vergleichsprüfungen</h3>
  <p class="para">
   Es ist möglich, einen <code class="literal">match</code>-Ausdruck zu verwenden,
   um nicht-vergleichende Fälle zu behandeln, indem <code class="code">true</code> als
   Bedingung verwendet wird.
  </p>

  <div class="example" id="example-5">
   <p><strong>Beispiel #5 Prüfung von ganzzahligen Bereichen</strong></p>
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /><br />$alter </span><span style="color: #007700">= </span><span style="color: #0000BB">23</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">$result </span><span style="color: #007700">= match (</span><span style="color: #0000BB">true</span><span style="color: #007700">) {<br />    </span><span style="color: #0000BB">$alter </span><span style="color: #007700">&gt;= </span><span style="color: #0000BB">65 </span><span style="color: #007700">=&gt; </span><span style="color: #DD0000">'Senior'</span><span style="color: #007700">,<br />    </span><span style="color: #0000BB">$alter </span><span style="color: #007700">&gt;= </span><span style="color: #0000BB">25 </span><span style="color: #007700">=&gt; </span><span style="color: #DD0000">'Erwachsener'</span><span style="color: #007700">,<br />    </span><span style="color: #0000BB">$alter </span><span style="color: #007700">&gt;= </span><span style="color: #0000BB">18 </span><span style="color: #007700">=&gt; </span><span style="color: #DD0000">'Junger Erwachsener'</span><span style="color: #007700">,<br />    default =&gt; </span><span style="color: #DD0000">'Kind'</span><span style="color: #007700">,<br />};<br /><br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$result</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
   </div>

   <div class="example-contents"><p>Das oben gezeigte Beispiel erzeugt folgende Ausgabe:</p></div>
   <div class="example-contents screen">
<div class="cdata"><pre>
string(18) &quot;Junger Erwachsener&quot;
</pre></div>
   </div>
  </div>

  <div class="example" id="example-6">
   <p><strong>Beispiel #6 Prüfung eines Textes auf enthaltene Zeichenfolgen</strong></p>
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /><br />$text </span><span style="color: #007700">= </span><span style="color: #DD0000">'Bienvenue chez nous'</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">$result </span><span style="color: #007700">= match (</span><span style="color: #0000BB">true</span><span style="color: #007700">) {<br />    </span><span style="color: #0000BB">str_contains</span><span style="color: #007700">(</span><span style="color: #0000BB">$text</span><span style="color: #007700">, </span><span style="color: #DD0000">'Welcome'</span><span style="color: #007700">), </span><span style="color: #0000BB">str_contains</span><span style="color: #007700">(</span><span style="color: #0000BB">$text</span><span style="color: #007700">, </span><span style="color: #DD0000">'Hello'</span><span style="color: #007700">) =&gt; </span><span style="color: #DD0000">'en'</span><span style="color: #007700">,<br />    </span><span style="color: #0000BB">str_contains</span><span style="color: #007700">(</span><span style="color: #0000BB">$text</span><span style="color: #007700">, </span><span style="color: #DD0000">'Bienvenue'</span><span style="color: #007700">), </span><span style="color: #0000BB">str_contains</span><span style="color: #007700">(</span><span style="color: #0000BB">$text</span><span style="color: #007700">, </span><span style="color: #DD0000">'Bonjour'</span><span style="color: #007700">) =&gt; </span><span style="color: #DD0000">'fr'</span><span style="color: #007700">,<br />    </span><span style="color: #FF8000">// ...<br /></span><span style="color: #007700">};<br /><br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$result</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
   </div>

   <div class="example-contents"><p>Das oben gezeigte Beispiel erzeugt folgende Ausgabe:</p></div>
   <div class="example-contents screen">
<div class="cdata"><pre>
string(2) &quot;fr&quot;
</pre></div>
   </div>
  </div>
 </div>
</div><?php manual_footer($setup); ?>