<?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 => 'it',
  ),
  'this' => 
  array (
    0 => 'control-structures.switch.php',
    1 => 'switch',
    2 => 'switch',
  ),
  'up' => 
  array (
    0 => 'language.control-structures.php',
    1 => 'Strutture di controllo',
  ),
  'prev' => 
  array (
    0 => 'control-structures.continue.php',
    1 => 'continue',
  ),
  'next' => 
  array (
    0 => 'control-structures.match.php',
    1 => 'match',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'en',
    '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">
  The <code class="literal">switch</code> statement is similar to a series of
  IF statements on the same expression.  In many occasions, you may
  want to compare the same variable (or expression) with many
  different values, and execute a different piece of code depending
  on which value it equals to.  This is exactly what the
  <code class="literal">switch</code> statement is for.
 </p>
 <blockquote class="note"><p><strong class="note">Nota</strong>: 
  <span class="simpara">
   Note that unlike some other languages, the
   <a href="control-structures.continue.php" class="link">continue</a> statement
   applies to <code class="literal">switch</code> and acts similar to <code class="literal">break</code>.  If you
   have a <code class="literal">switch</code> inside a loop and wish to continue to the next iteration of
   the outer loop, use <code class="literal">continue 2</code>.
  </span>
 </p></blockquote>
 <blockquote class="note"><p><strong class="note">Nota</strong>: 
  <p class="para">
   Note that switch/case does
   <a href="types.comparisons.php#types.comparisions-loose" class="link">loose comparison</a>.
  </p>
 </p></blockquote>

 <p class="para">
  In the following example, each code block is equivalent.
  One uses a series of <code class="literal">if</code> and
  <code class="literal">elseif</code> statements, and the other a
  <code class="literal">switch</code> statement.  In each case, the output is the same.
  <div class="example" id="example-1">
   <p><strong>Example #1 <code class="literal">switch</code> structure</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 equals 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 equals 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 equals 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 equals 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 equals 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 equals 2"</span><span style="color: #007700">;<br />}<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
   </div>

  </div>
 </p>
 <p class="para">
  It is important to understand how the <code class="literal">switch</code>
  statement is executed in order to avoid mistakes.  The
  <code class="literal">switch</code> statement executes line by line
  (actually, statement by statement).  In the beginning, no code is
  executed.  Only when a <code class="literal">case</code> statement is found
  whose expression evaluates to a value that matches the value of the
  <code class="literal">switch</code> expression does PHP begin to execute the
  statements.  PHP continues to execute the statements until the end
  of the <code class="literal">switch</code> block, or the first time it sees
  a <code class="literal">break</code> statement.  If you don&#039;t write a
  <code class="literal">break</code> statement at the end of a case&#039;s
  statement list, PHP will go on executing the statements of the
  following case.  For example:
  <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 equals 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 equals 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 equals 2"</span><span style="color: #007700">;<br />}<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
   </div>

  </div>
 </p>
 <p class="simpara">
  Here, if <var class="varname">$i</var> is equal to 0, PHP would execute all of the echo
  statements!  If <var class="varname">$i</var> is equal to 1, PHP would execute the last two
  echo statements. You would get the expected behavior (&#039;i equals 2&#039;
  would be displayed) only if <var class="varname">$i</var> is equal to 2.  Thus,
  it is important not to forget <code class="literal">break</code> statements
  (even though you may want to avoid supplying them on purpose under
  certain circumstances).
 </p>
 <p class="simpara">
  In a <code class="literal">switch</code> statement, the condition is
  evaluated only once and the result is compared to each
  <code class="literal">case</code> statement. In an <code class="literal">elseif</code>
  statement, the condition is evaluated again. If your condition is
  more complicated than a simple compare and/or is in a tight loop,
  a <code class="literal">switch</code> may be faster.
 </p>
 <p class="para">
  The statement list for a case can also be empty, which simply
  passes control into the statement list for the next case.
  <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 is less than 3 but not negative"</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 is 3"</span><span style="color: #007700">;<br />}<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
   </div>

  </div>
 </p>
 <p class="para">
  A special case is the <code class="literal">default</code> case.  This case matches
  anything that wasn&#039;t matched by the other cases.  For example:
  <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 equals 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 equals 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 equals 2"</span><span style="color: #007700">;<br />        break;<br />    default:<br />       echo </span><span style="color: #DD0000">"i is not equal to 0, 1 or 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">Nota</strong>: 
   <span class="simpara">
    Multiple default cases will raise a
    <strong><code><a href="errorfunc.constants.php#constant.e-compile-error">E_COMPILE_ERROR</a></code></strong> error.
   </span>
  </p></blockquote>
  <blockquote class="note"><p><strong class="note">Nota</strong>: 
   <span class="simpara">
    Technically the <code class="literal">default</code> case may be listed
    in any order. It will only be used if no other case matches.
    However, by convention it is best to place it at the end as the
    last branch.
   </span>
  </p></blockquote>
 </p>
 <p class="para">
  If no <code class="literal">case</code> branch matches, and there is no <code class="literal">default</code>
  branch, then no code will be executed, just as if no <code class="literal">if</code> statement was true.
 </p>
 <p class="para">
  A case value may be given as an expression. However, that expression will be
  evaluated on its own and then loosely compared with the switch value. That means
  it cannot be used for complex evaluations of the switch value.  For example:
  <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">// Prints "B"<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
   </div>

  </div>
 </p>
 <p class="para">
  For more complex comparisons, the value <strong><code><a href="reserved.constants.php#constant.true">true</a></code></strong> may be used as the switch value.
  Or, alternatively, <code class="literal">if</code>-<code class="literal">else</code> blocks instead of <code class="literal">switch</code>.
  <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">// Prints "B"<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
   </div>

  </div>
 </p>
 <p class="para">
  The alternative syntax for control structures is supported with
  switches. For more information, see <a href="control-structures.alternative-syntax.php" class="link">Alternative syntax
  for control structures</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 equals 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 equals 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 equals 2"</span><span style="color: #007700">;<br />        break;<br />    default:<br />        echo </span><span style="color: #DD0000">"i is not equal to 0, 1 or 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">
  It&#039;s possible to use a semicolon instead of a colon after a case like:
  <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">'tuborg'</span><span style="color: #007700">;<br />    case </span><span style="color: #DD0000">'carlsberg'</span><span style="color: #007700">;<br />    case </span><span style="color: #DD0000">'stella'</span><span style="color: #007700">;<br />    case </span><span style="color: #DD0000">'heineken'</span><span style="color: #007700">;<br />        echo </span><span style="color: #DD0000">'Good choice'</span><span style="color: #007700">;<br />        break;<br />    default;<br />        echo </span><span style="color: #DD0000">'Please make a new selection...'</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">Vedere anche:</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); ?>