<?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.switch.php',
    1 => 'switch',
    2 => 'switch',
  ),
  'up' => 
  array (
    0 => 'language.control-structures.php',
    1 => 'Kontrollstrukturen',
  ),
  'prev' => 
  array (
    0 => 'control-structures.continue.php',
    1 => 'continue',
  ),
  'next' => 
  array (
    0 => 'control-structures.match.php',
    1 => 'match',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'de',
    'path' => 'language/control-structures/switch.xml',
  ),
  'history' => 
  array (
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="control-structures.switch" class="sect1">
 <h2 class="title">switch</h2>
 <p class="verinfo">(PHP 4, PHP 5, PHP 7, PHP 8)</p>
 <p class="simpara">
  Die <code class="literal">switch</code>-Anweisung entspricht in etwa einer
  Folge von <code class="literal">if</code>-Anweisungen die jeweils den gleichen
  Ausdruck prüfen. Es kommt oft vor, dass man dieselbe Variable (oder
  denselben Ausdruck) gegen viele verschiedene mögliche Werte prüfen
  und abhängig davon unterschiedlichen Code ausführen möchte. Genau
  hierfür wurde die <code class="literal">switch</code>-Anweisung eingeführt.
 </p>
 <blockquote class="note"><p><strong class="note">Hinweis</strong>: 
  <span class="simpara">
   Beachten Sie, dass im Unterschied zu einigen anderen Sprachen die
   <a href="control-structures.continue.php" class="link">continue</a>-Anweisung
   auch auf <code class="literal">switch</code> anwendbar ist und sich ähnlich wie <code class="literal">break</code>
   verhält. Wenn Sie ein <code class="literal">switch</code> innerhalb einer Schleife verwenden und
   mit dem nächsten Schleifendurchlauf beginnen wollen so müssen Sie in
   diesem Fall <code class="literal">continue 2</code> angeben.
  </span>
 </p></blockquote>
 <blockquote class="note"><p><strong class="note">Hinweis</strong>: 
  <p class="para">
   Beachten Sie auch, dass switch/case
   <a href="types.comparisons.php#types.comparisions-loose" class="link">typschwache Vergleiche</a>
   durchführt.
  </p>
 </p></blockquote>
 <p class="para">
  Im folgenden Beispiel sind die beiden Codeblöcke gleichwertig.
  Der eine verwendet eine Reihe von <code class="literal">if</code>- und
  <code class="literal">elseif</code>-Anweisungen und der andere eine
  <code class="literal">switch</code>-Anweisung. Die Ausgabe ist in beiden Fällen
  dieselbe.
  <div class="example" id="example-1">
   <p><strong>Beispiel #1 <code class="literal">switch</code>-Anweisung</strong></p>
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">// This switch statement:<br /><br /></span><span style="color: #007700">switch (</span><span style="color: #0000BB">$i</span><span style="color: #007700">) {<br />    case </span><span style="color: #0000BB">0</span><span style="color: #007700">:<br />        echo </span><span style="color: #DD0000">"i ist gleich 0"</span><span style="color: #007700">;<br />        break;<br />    case </span><span style="color: #0000BB">1</span><span style="color: #007700">:<br />        echo </span><span style="color: #DD0000">"i ist gleich 1"</span><span style="color: #007700">;<br />        break;<br />    case </span><span style="color: #0000BB">2</span><span style="color: #007700">:<br />        echo </span><span style="color: #DD0000">"i ist gleich 2"</span><span style="color: #007700">;<br />        break;<br />}<br /><br /></span><span style="color: #FF8000">// Is equivalent to:<br /><br /></span><span style="color: #007700">if (</span><span style="color: #0000BB">$i </span><span style="color: #007700">== </span><span style="color: #0000BB">0</span><span style="color: #007700">) {<br />    echo </span><span style="color: #DD0000">"i ist gleich 0"</span><span style="color: #007700">;<br />} elseif (</span><span style="color: #0000BB">$i </span><span style="color: #007700">== </span><span style="color: #0000BB">1</span><span style="color: #007700">) {<br />    echo </span><span style="color: #DD0000">"i ist gleich 1"</span><span style="color: #007700">;<br />} elseif (</span><span style="color: #0000BB">$i </span><span style="color: #007700">== </span><span style="color: #0000BB">2</span><span style="color: #007700">) {<br />    echo </span><span style="color: #DD0000">"i ist gleich 2"</span><span style="color: #007700">;<br />}<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
   </div>

  </div>
 </p>
 <p class="para">
  Um Fehler zu vermeiden ist es wichtig zu verstehen wie eine
  <code class="literal">switch</code>-Anweisung ausgeführt wird. Eine
  <code class="literal">switch</code>-Anweisung wird zeilenweise (genauer: Anweisung für
  Anweisung) ausgewertet. Zunächst einmal wird überhaupt kein Code ausgeführt.
  Erst wenn eine <code class="literal">case</code>-Anweisung gefunden wird, deren
  Ausdruck zu einem Wert evaluiert, der dem an <code class="literal">switch</code>
  übergebenen Ausdruck entspricht, beginnt PHP tatsächlich die folgenden
  Anweisungen auszuführen. Die Ausführung wird fortgesetzt bis das Ende
  der <code class="literal">switch</code>-Anweisung erreicht ist oder bis PHP
  bei der Ausführung auf eine <code class="literal">break</code>-Anweisung stößt.
  Wenn Sie am Ende der Anweisungsliste eines case kein
  <code class="literal">break</code> setzen, so wird die Ausführung mit dem nächsten
  case fortgesetzt, z. B.:
  <div class="informalexample">
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">switch (</span><span style="color: #0000BB">$i</span><span style="color: #007700">) {<br />    case </span><span style="color: #0000BB">0</span><span style="color: #007700">:<br />        echo </span><span style="color: #DD0000">"i ist gleich 0"</span><span style="color: #007700">;<br />    case </span><span style="color: #0000BB">1</span><span style="color: #007700">:<br />        echo </span><span style="color: #DD0000">"i ist gleich 1"</span><span style="color: #007700">;<br />    case </span><span style="color: #0000BB">2</span><span style="color: #007700">:<br />        echo </span><span style="color: #DD0000">"i ist gleich 2"</span><span style="color: #007700">;<br />}<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
   </div>

  </div>
 </p>
 <p class="simpara">
  Wenn in diesem Beispiel <var class="varname">$i</var> gleich 0 ist, führt PHP alle
  echo-Anweisungen aus! Ist <var class="varname">$i</var> gleich 1, führt PHP die
  letzten beiden echo-Anweisungen aus. Sie erreichen das erwartete Verhalten
  nur, wenn <var class="varname">$i</var> gleich 2 ist. Daher ist es wichtig
  <code class="literal">break</code>-Anweisungen nicht zu vergessen (auch wenn Sie
  gelegentlich absichtlich darauf verzichten werden, diese unter bestimmten
  Bedingungen anzugeben).
 </p>
 <p class="simpara">
  In einer <code class="literal">switch</code>-Anweisung wird die Bedingung nur einmal
  ausgewertet und das Ergebnis mit jeder <code class="literal">case</code>-Anweisung
  verglichen. In einer <code class="literal">elseif</code>-Anweisung wird die Bedingung
  erneut ausgewertet. Wenn Ihre Bedingung komplizierter ist als ein einfacher
  Vergleich, und/oder sich in einer dichten Schleife befindet, kann ein
  <code class="literal">switch</code> schneller sein.
 </p>
 <p class="para">
  Die Anweisungsliste eines case kann ebenso leer sein, was einfach die
  Kontrolle an die Anweisungsliste des nächsten case übergibt.
  <div class="informalexample">
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">switch (</span><span style="color: #0000BB">$i</span><span style="color: #007700">) {<br />    case </span><span style="color: #0000BB">0</span><span style="color: #007700">:<br />    case </span><span style="color: #0000BB">1</span><span style="color: #007700">:<br />    case </span><span style="color: #0000BB">2</span><span style="color: #007700">:<br />        echo </span><span style="color: #DD0000">"i ist kleiner als 3, aber nicht negativ"</span><span style="color: #007700">;<br />        break;<br />    case </span><span style="color: #0000BB">3</span><span style="color: #007700">:<br />        echo </span><span style="color: #DD0000">"i ist 3"</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 alles
  ab, was nicht durch eine der anderen case-Klauseln behandelt wurde. Zum
  Beispiel:
  <div class="informalexample">
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">switch (</span><span style="color: #0000BB">$i</span><span style="color: #007700">) {<br />    case </span><span style="color: #0000BB">0</span><span style="color: #007700">:<br />        echo </span><span style="color: #DD0000">"i ist gleich 0"</span><span style="color: #007700">;<br />        break;<br />    case </span><span style="color: #0000BB">1</span><span style="color: #007700">:<br />        echo </span><span style="color: #DD0000">"i ist gleich 1"</span><span style="color: #007700">;<br />        break;<br />    case </span><span style="color: #0000BB">2</span><span style="color: #007700">:<br />        echo </span><span style="color: #DD0000">"i ist gleich 2"</span><span style="color: #007700">;<br />        break;<br />    default:<br />       echo </span><span style="color: #DD0000">"i ist nicht gleich 0, 1 oder 2"</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 case-Anweisungen erzeugen einen
    <strong><code><a href="errorfunc.constants.php#constant.e-compile-error">E_COMPILE_ERROR</a></code></strong>-Fehler.
   </span>
  </p></blockquote>
  <blockquote class="note"><p><strong class="note">Hinweis</strong>: 
   <span class="simpara">
    Technisch gesehen kann der <code class="literal">default</code>-Case an einer
    beliebigen Stelle stehen. Er wird nur verwendet, wenn kein anderer Fall
    passt. Nach gängiger Praxis ist es jedoch am besten, ihn als letzten Punkt
    ans Ende zu setzen.
   </span>
  </p></blockquote>
 </p>
 <p class="para">
  Wenn kein <code class="literal">case</code>-Zweig passt und es keinen
  <code class="literal">default</code>-Zweig gibt, dann wird kein Code ausgeführt,
  genauso wie wenn keine <code class="literal">if</code>-Anweisung <strong><code><a href="reserved.constants.php#constant.true">true</a></code></strong> wäre.
 </p>
 <p class="para">
  Ein case-Wert kann als Ausdruck angegeben werden. Dieser Ausdruck wird
  jedoch für sich allein ausgewertet und dann nur einfach mit dem switch-Wert
  verglichen. Das bedeutet, dass er nicht für komplexe Auswertungen des
  switch-Wertes verwendet werden kann. Zum Beispiel:
  <div class="informalexample">
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />$target </span><span style="color: #007700">= </span><span style="color: #0000BB">1</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$start </span><span style="color: #007700">= </span><span style="color: #0000BB">3</span><span style="color: #007700">;<br /><br />switch (</span><span style="color: #0000BB">$target</span><span style="color: #007700">) {<br />    case </span><span style="color: #0000BB">$start </span><span style="color: #007700">- </span><span style="color: #0000BB">1</span><span style="color: #007700">:<br />        print </span><span style="color: #DD0000">"A"</span><span style="color: #007700">;<br />        break;<br />    case </span><span style="color: #0000BB">$start </span><span style="color: #007700">- </span><span style="color: #0000BB">2</span><span style="color: #007700">:<br />        print </span><span style="color: #DD0000">"B"</span><span style="color: #007700">;<br />        break;<br />    case </span><span style="color: #0000BB">$start </span><span style="color: #007700">- </span><span style="color: #0000BB">3</span><span style="color: #007700">:<br />        print </span><span style="color: #DD0000">"C"</span><span style="color: #007700">;<br />        break;<br />    case </span><span style="color: #0000BB">$start </span><span style="color: #007700">- </span><span style="color: #0000BB">4</span><span style="color: #007700">:<br />        print </span><span style="color: #DD0000">"D"</span><span style="color: #007700">;<br />        break;<br />}<br /><br /></span><span style="color: #FF8000">// Gibt "B" aus<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
   </div>

  </div>
 </p>
 <p class="para">
  Für komplexere Vergleiche kann der Wert <strong><code><a href="reserved.constants.php#constant.true">true</a></code></strong> als Switch-Wert verwendet
  werden. Statt <code class="literal">switch</code> können alternativ auch
  <code class="literal">if</code>-<code class="literal">else</code>-Blöcke verwendet werden.
  <div class="informalexample">
   <div class="example-contents">
    <div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />$offset </span><span style="color: #007700">= </span><span style="color: #0000BB">1</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$start </span><span style="color: #007700">= </span><span style="color: #0000BB">3</span><span style="color: #007700">;<br /><br />switch (</span><span style="color: #0000BB">true</span><span style="color: #007700">) {<br />    case </span><span style="color: #0000BB">$start </span><span style="color: #007700">- </span><span style="color: #0000BB">$offset </span><span style="color: #007700">=== </span><span style="color: #0000BB">1</span><span style="color: #007700">:<br />        print </span><span style="color: #DD0000">"A"</span><span style="color: #007700">;<br />        break;<br />    case </span><span style="color: #0000BB">$start </span><span style="color: #007700">- </span><span style="color: #0000BB">$offset </span><span style="color: #007700">=== </span><span style="color: #0000BB">2</span><span style="color: #007700">:<br />        print </span><span style="color: #DD0000">"B"</span><span style="color: #007700">;<br />        break;<br />    case </span><span style="color: #0000BB">$start </span><span style="color: #007700">- </span><span style="color: #0000BB">$offset </span><span style="color: #007700">=== </span><span style="color: #0000BB">3</span><span style="color: #007700">:<br />        print </span><span style="color: #DD0000">"C"</span><span style="color: #007700">;<br />        break;<br />    case </span><span style="color: #0000BB">$start </span><span style="color: #007700">- </span><span style="color: #0000BB">$offset </span><span style="color: #007700">=== </span><span style="color: #0000BB">4</span><span style="color: #007700">:<br />        print </span><span style="color: #DD0000">"D"</span><span style="color: #007700">;<br />        break;<br />}<br /><br /></span><span style="color: #FF8000">// Gibt "B" aus<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
   </div>

  </div>
 </p>
 <p class="para">
  Die alternative Syntax für Kontrollstrukturen wird von switch-Anweisungen
  unterstützt. Weitere Information entnehmen Sie dem Abschnitt
  <a href="control-structures.alternative-syntax.php" class="link">Alternative Syntax für Kontrollstrukturen</a>.
  <div class="informalexample">
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">switch (</span><span style="color: #0000BB">$i</span><span style="color: #007700">):<br />    case </span><span style="color: #0000BB">0</span><span style="color: #007700">:<br />        echo </span><span style="color: #DD0000">"i ist gleich 0"</span><span style="color: #007700">;<br />        break;<br />    case </span><span style="color: #0000BB">1</span><span style="color: #007700">:<br />        echo </span><span style="color: #DD0000">"i ist gleich 1"</span><span style="color: #007700">;<br />        break;<br />    case </span><span style="color: #0000BB">2</span><span style="color: #007700">:<br />        echo </span><span style="color: #DD0000">"i ist gleich 2"</span><span style="color: #007700">;<br />        break;<br />    default:<br />        echo </span><span style="color: #DD0000">"i ist nicht gleich 0, 1 oder 2"</span><span style="color: #007700">;<br />endswitch;<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
   </div>

  </div>
 </p>
 <p class="para">
  Es ist möglich, ein Semikolon anstelle eines Doppelpunkts nach einem case zu
  verwenden, wie:
  <div class="informalexample">
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">switch(</span><span style="color: #0000BB">$beer</span><span style="color: #007700">)<br />{<br />    case </span><span style="color: #DD0000">'Bitburger'</span><span style="color: #007700">;<br />    case </span><span style="color: #DD0000">'Erdinger'</span><span style="color: #007700">;<br />    case </span><span style="color: #DD0000">'Jever'</span><span style="color: #007700">;<br />        echo </span><span style="color: #DD0000">'Gute Wahl'</span><span style="color: #007700">;<br />        break;<br />    default;<br />        echo </span><span style="color: #DD0000">'Bitte treffen Sie eine andere Wahl...'</span><span style="color: #007700">;<br />        break;<br />}<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
   </div>

  </div>
 </p>

 <div class="sect2">
  <h3 class="title">Siehe auch</h3>
  <p class="para">
   <ul class="simplelist">
    <li><a href="control-structures.match.php" class="link">match</a></li>
   </ul>
  </p>
 </div>
</div><?php manual_footer($setup); ?>