-
Notifications
You must be signed in to change notification settings - Fork 101
/
goto.xml
131 lines (123 loc) · 2.87 KB
/
goto.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
<?xml version="1.0" encoding="utf-8"?>
<!-- $Revision$ -->
<!-- $Author$ -->
<!-- EN-Revision: 7204e2dbb9b484c8b67bb5ad4a93fa1369c5b317 Maintainer: HaoHappy Status: ready -->
<!-- CREDITS: Luffy -->
<sect1 xml:id="control-structures.goto" xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink">
<title>goto</title>
<?phpdoc print-version-for="goto"?>
<para>
<mediaobject>
<alt>What's the worse thing that could happen if you use goto?</alt>
<imageobject>
<imagedata fileref="en/language/figures/xkcd-goto.png" format="PNG"/>
</imageobject>
<caption>
<simpara>
此漫画鸣谢 <link xlink:href="&url.xkcd;292">xkcd</link>
</simpara>
</caption>
</mediaobject>
</para>
<para>
<literal>goto</literal>
操作符可以用来跳转到程序中的另一位置。该目标位置可以用 <emphasis>区分大小写</emphasis> 的目标名称加上冒号来标记,而跳转指令是
<literal>goto</literal> 之后接上目标位置的标记。PHP 中的 <literal>goto</literal>
有一定限制,目标位置只能位于同一个文件和作用域,也就是说无法跳出一个函数或类方法,也无法跳入到另一个函数。也无法跳入到任何循环或者
switch 结构中。可以跳出循环或者 switch,通常的用法是用 <literal>goto</literal>
代替多层的 <literal>break</literal>。
</para>
<para>
<example>
<title><literal>goto</literal> 示例</title>
<programlisting role="php">
<![CDATA[
<?php
goto a;
echo 'Foo';
a:
echo 'Bar';
?>
]]>
</programlisting>
&example.outputs;
<screen>
<![CDATA[
Bar
]]>
</screen>
</example>
</para>
<para>
<example>
<title><literal>goto</literal> 跳出循环示例</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>以下写法无效</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>
</sect1>
<!-- Keep this comment at the end of the file
Local variables:
mode: sgml
sgml-omittag:t
sgml-shorttag:t
sgml-minimize-attributes:nil
sgml-always-quote-attributes:t
sgml-indent-step:1
sgml-indent-data:t
indent-tabs-mode:nil
sgml-parent-document:nil
sgml-default-dtd-file:"~/.phpdoc/manual.ced"
sgml-exposed-tags:nil
sgml-local-catalogs:nil
sgml-local-ecat-files:nil
End:
vim600: syn=xml fen fdm=syntax fdl=2 si
vim: et tw=78 syn=sgml
vi: ts=1 sw=1
-->