cvs: phpdoc /en/language/control-structures goto.xml
| From: | Rasmus Lerdorf | Date: | Sat, 20 Jun 2009 18:07:12 +0000 |
| Subject: | cvs: phpdoc /en/language/control-structures goto.xml | ||
| Groups: | php.doc.cvs | ||
| Request: | Send a blank email to doc-cvs+get-4173@lists.php.net to get a copy of this message | ||
rasmus Sat Jun 20 18:07:12 2009 UTC
Modified files:
/phpdoc/en/language/control-structures goto.xml
Log:
Improve goto description and add a couple of examples. Remove the
warning because I think it is better to simply describe that
restriction up front.
http://cvs.php.net/viewvc.cgi/phpdoc/en/language/control-structures/goto.xml?r1=1.3&r2=1.4&diff_format=u
Index: phpdoc/en/language/control-structures/goto.xml
diff -u phpdoc/en/language/control-structures/goto.xml:1.3
phpdoc/en/language/control-structures/goto.xml:1.4
--- phpdoc/en/language/control-structures/goto.xml:1.3 Sun Jun 14 22:03:29 2009
+++ phpdoc/en/language/control-structures/goto.xml Sat Jun 20 18:07:12 2009
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
-<!-- $Revision: 1.3 $ -->
+<!-- $Revision: 1.4 $ -->
<sect1 xml:id="control-structures.goto" xmlns="http://docbook.org/ns/docbook"
xmlns:xlink="http://www.w3.org/1999/xlink">
<title><literal>goto</literal></title>
@@ -7,7 +7,13 @@
The <literal>goto</literal> operator can be used to jump to another
section in the program. The target point is specified by a label
followed by a colon, and the instruction is given as
- <literal>goto</literal> followed by the desired target label.
+ <literal>goto</literal> followed by the desired target label. This
+ is not a full unrestricted <literal>goto</literal>. The target
+ label must be within the same context, meaning that you cannot jump
+ out of a function or method, nor can you jump into one. You also
+ cannot jump into any sort of loop or switch structure. You may jump
+ out of these, and a common use is to use a <literal>goto</literal>
+ in place of a multi-level <literal>break</literal>.
</para>
<para>
<example>
@@ -31,17 +37,61 @@
</screen>
</example>
</para>
+ <para>
+ <example>
+ <title><literal>goto</literal> loop example</title>
+ <programlisting role="php">
+<![CDATA[
+<?php
+for($i=0,$j=50; $i<100; $i++) {
+ while($j--) {
+ if($j==17) goto end;
+ }
+}
+echo "i = $i";
+end:
+echo 'j hit 17';
+?>
+]]>
+ </programlisting>
+ &example.outputs;
+ <screen>
+<![CDATA[
+j hit 17
+]]>
+ </screen>
+ </example>
+ </para>
+ <para>
+ <example>
+ <title>This will not work</title>
+ <programlisting role="php">
+<![CDATA[
+<?php
+goto loop;
+for($i=0,$j=50; $i<100; $i++) {
+ while($j--) {
+ loop:
+ }
+}
+echo "$i = $i";
+?>
+]]>
+ </programlisting>
+ &example.outputs;
+ <screen>
+<![CDATA[
+Fatal error: 'goto' into loop or switch statement is disallowed in
+script on line 2
+]]>
+ </screen>
+ </example>
+ </para>
<note>
<para>
The <literal>goto</literal> operator is available as of PHP 5.3.
</para>
</note>
- <warning>
- <para>
- It is not allowed to jump into a loop or switch statement. A fatal
- error is issued in such cases.
- </para>
- </warning>
<para>
<mediaobject>
<imageobject>