“PHP中continue的使用”的版本间的差异

来自Blueidea
跳转至: 导航搜索
第8行: 第8行:
 
continue 接受一个可选的数字参数来决定跳过几重循环到循环结尾。  
 
continue 接受一个可选的数字参数来决定跳过几重循环到循环结尾。  
  
<source><?php  
+
<source language="php"><?php  
 
while (list ($key, $value) = each($arr)) {  
 
while (list ($key, $value) = each($arr)) {  
 
if (!($key % 2)) { // skip odd members  
 
if (!($key % 2)) { // skip odd members  
第33行: 第33行:
 
省略 continue 后面的分号会导致混淆。以下例子示意了不应该这样做。  
 
省略 continue 后面的分号会导致混淆。以下例子示意了不应该这样做。  
  
 +
<source language="php">
 
<?php  
 
<?php  
 
for ($i = 0; $i < 5; ++$i) {  
 
for ($i = 0; $i < 5; ++$i) {  
第39行: 第40行:
 
print "$i\n";  
 
print "$i\n";  
 
}  
 
}  
?>  
+
?></source>
 +
  
 
希望得到的结果是:  
 
希望得到的结果是:  

2007-09-06T16:31:08的版本

switch,while等都是PHP非常常用的循环,而能够用好循环,对于使用PHP来编写有较强逻辑性的程序来说是非常不错的。 break是跳出循环,而continue是进行下一次循环。 很多东西手册上有,最重要的是如何把他们运用到实际当中去。

以下为摘抄的部分

continue 在循环结构用用来跳过本次循环中剩余的代码并在条件求值为真时开始执行下一次循环。 continue 接受一个可选的数字参数来决定跳过几重循环到循环结尾。

无效的语言。

您需要指定一种语言,像这样: <source lang="html4strict">...</source>

语法高亮所支持的语言:

4cs, 6502acme, 6502kickass, 6502tasm, 68000devpac, abap, actionscript, actionscript3, ada, algol68, apache, applescript, apt_sources, arm, asm, asp, asymptote, autoconf, autohotkey, autoit, avisynth, awk, bascomavr, bash, basic4gl, bf, bibtex, blitzbasic, bnf, boo, c, c_loadrunner, c_mac, caddcl, cadlisp, cfdg, cfm, chaiscript, cil, clojure, cmake, cobol, coffeescript, cpp, cpp-qt, csharp, css, cuesheet, d, dcl, dcpu16, dcs, delphi, diff, div, dos, dot, e, ecmascript, eiffel, email, epc, erlang, euphoria, f1, falcon, fo, fortran, freebasic, freeswitch, fsharp, gambas, gdb, genero, genie, gettext, glsl, gml, gnuplot, go, groovy, gwbasic, haskell, haxe, hicest, hq9plus, html4strict, html5, icon, idl, ini, inno, intercal, io, j, java, java5, javascript, jquery, kixtart, klonec, klonecpp, latex, lb, ldif, lisp, llvm, locobasic, logtalk, lolcode, lotusformulas, lotusscript, lscript, lsl2, lua, m68k, magiksf, make, mapbasic, matlab, mirc, mmix, modula2, modula3, mpasm, mxml, mysql, nagios, netrexx, newlisp, nsis, oberon2, objc, objeck, ocaml, ocaml-brief, octave, oobas, oorexx, oracle11, oracle8, oxygene, oz, parasail, parigp, pascal, pcre, per, perl, perl6, pf, php, php-brief, pic16, pike, pixelbender, pli, plsql, postgresql, povray, powerbuilder, powershell, proftpd, progress, prolog, properties, providex, purebasic, pycon, pys60, python, q, qbasic, rails, rebol, reg, rexx, robots, rpmspec, rsplus, ruby, sas, scala, scheme, scilab, sdlbasic, smalltalk, smarty, spark, sparql, sql, stonescript, systemverilog, tcl, teraterm, text, thinbasic, tsql, typoscript, unicon, upc, urbi, uscript, vala, vb, vbnet, vedit, verilog, vhdl, vim, visualfoxpro, visualprolog, whitespace, whois, winbatch, xbasic, xml, xorg_conf, xpp, yaml, z80, zxbasic


<?php 
while (list ($key, $value) = each($arr)) { 
if (!($key % 2)) { // skip odd members 
continue; 
} 
do_something_odd($value); 
} 

$i = 0; 
while ($i++ < 5) { 
echo "Outer<br />\n"; 
while (1) { 
echo "  Middle<br />\n"; 
while (1) { 
echo "  Inner<br />\n"; 
continue 3; 
} 
echo "This never gets output.<br />\n"; 
} 
echo "Neither does this.<br />\n"; 
} 
?>

省略 continue 后面的分号会导致混淆。以下例子示意了不应该这样做。

无效的语言。

您需要指定一种语言,像这样: <source lang="html4strict">...</source>

语法高亮所支持的语言:

4cs, 6502acme, 6502kickass, 6502tasm, 68000devpac, abap, actionscript, actionscript3, ada, algol68, apache, applescript, apt_sources, arm, asm, asp, asymptote, autoconf, autohotkey, autoit, avisynth, awk, bascomavr, bash, basic4gl, bf, bibtex, blitzbasic, bnf, boo, c, c_loadrunner, c_mac, caddcl, cadlisp, cfdg, cfm, chaiscript, cil, clojure, cmake, cobol, coffeescript, cpp, cpp-qt, csharp, css, cuesheet, d, dcl, dcpu16, dcs, delphi, diff, div, dos, dot, e, ecmascript, eiffel, email, epc, erlang, euphoria, f1, falcon, fo, fortran, freebasic, freeswitch, fsharp, gambas, gdb, genero, genie, gettext, glsl, gml, gnuplot, go, groovy, gwbasic, haskell, haxe, hicest, hq9plus, html4strict, html5, icon, idl, ini, inno, intercal, io, j, java, java5, javascript, jquery, kixtart, klonec, klonecpp, latex, lb, ldif, lisp, llvm, locobasic, logtalk, lolcode, lotusformulas, lotusscript, lscript, lsl2, lua, m68k, magiksf, make, mapbasic, matlab, mirc, mmix, modula2, modula3, mpasm, mxml, mysql, nagios, netrexx, newlisp, nsis, oberon2, objc, objeck, ocaml, ocaml-brief, octave, oobas, oorexx, oracle11, oracle8, oxygene, oz, parasail, parigp, pascal, pcre, per, perl, perl6, pf, php, php-brief, pic16, pike, pixelbender, pli, plsql, postgresql, povray, powerbuilder, powershell, proftpd, progress, prolog, properties, providex, purebasic, pycon, pys60, python, q, qbasic, rails, rebol, reg, rexx, robots, rpmspec, rsplus, ruby, sas, scala, scheme, scilab, sdlbasic, smalltalk, smarty, spark, sparql, sql, stonescript, systemverilog, tcl, teraterm, text, thinbasic, tsql, typoscript, unicon, upc, urbi, uscript, vala, vb, vbnet, vedit, verilog, vhdl, vim, visualfoxpro, visualprolog, whitespace, whois, winbatch, xbasic, xml, xorg_conf, xpp, yaml, z80, zxbasic


<?php 
for ($i = 0; $i < 5; ++$i) { 
if ($i == 2) 
continue 
print "$i\n"; 
} 
?>


希望得到的结果是:

0 1 3 4

可实际的输出是: 2