[prev in list] [next in list] [prev in thread] [next in thread] 

List:       php-doc-cvs
Subject:    [DOC-CVS] [doc-en] master: Expand switch description and examples to deprecate comments.
From:       "Larry Garfield via Christoph M. Becker" <noreply () php ! net>
Date:       2022-06-29 17:55:39
Message-ID: S1LckbJIcSQxP0C7EY0zi4B0t7s8Y2g7X9byeeXJ9RY () main ! php ! net
[Download RAW message or body]

Author: Larry Garfield (Crell)
Committer: Christoph M. Becker (cmb69)
Date: 2022-06-29T17:28:54+02:00

Commit: https://github.com/php/doc-en/commit/3a5a6f1970b146e80cdc8c50319922abb90b18ae
Raw diff: https://github.com/php/doc-en/commit/3a5a6f1970b146e80cdc8c50319922abb90b18ae.diff

Expand switch description and examples to deprecate comments.

Changed paths:
  M  language/control-structures/switch.xml


Diff:

diff --git a/language/control-structures/switch.xml b/language/control-structures/switch.xml
index 6b985d941fe..1e7ff77f84e 100644
--- a/language/control-structures/switch.xml
+++ b/language/control-structures/switch.xml
@@ -66,6 +66,14 @@ switch ($i) {
    <programlisting role="php">
 <![CDATA[
 <?php
+if ($i == 'apple') {
+    echo "i is apple";
+} elseif ($i == 'bar') {
+    echo "i is bar";
+} elseif ($i == 'cake') {
+    echo "i is cake";
+}
+
 switch ($i) {
     case "apple":
         echo "i is apple";
@@ -181,6 +189,77 @@ switch ($i) {
     <constant>E_COMPILE_ERROR</constant> error.
    </simpara>
   </note>
+  <note>
+   <simpara>
+    Technically the <literal>default</literal> 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.
+   </simpara>
+  </note>
+ </para>
+ <para>
+  If no <literal>case</literal> branch matches, and there is no <literal>default</literal>
+  branch, then no code will be executed, just as if no <literal>if</literal> statement was true.
+ </para>
+ <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:
+  <informalexample>
+   <programlisting role="php">
+<![CDATA[
+<?php
+$target = 1;
+$start = 3;
+
+switch ($target) {
+    case $start - 1:
+        print "A";
+        break;
+    case $start - 2:
+        print "B";
+        break;
+    case $start - 3:
+        print "C";
+        break;
+    case $start - 4:
+        print "C";
+        break;
+}
+?>
+]]>
+   </programlisting>
+  </informalexample>
+ </para>
+ <para>
+  For more complex comparisons, use the value <literal>true</literal> as the switch value.
+  Or, alternatively, use <literal>if</literal>-<literal>else</literal> blocks.
+  <informalexample>
+   <programlisting role="php">
+    <![CDATA[
+<?php
+$offset = 1;
+$start = 3;
+
+switch (true) {
+    case $start - $offset === 1:
+        print "A";
+        break;
+    case $start - $offset === 2:
+        print "B";
+        break;
+    case $start - $offset === 3:
+        print "C";
+        break;
+    case $start - $offset === 4:
+        print "C";
+        break;
+}
+?>
+]]>
+   </programlisting>
+  </informalexample>
  </para>
  <para>
   The alternative syntax for control structures is supported with

-- 
PHP Documentation Commits Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic