相关文章推荐
小百科
›
PHP中的循环和它们的类型
php
递归
胡子拉碴的绿豆
10 月前
</noscript><div id="__next"><style data-emotion="css zf0iqh">.css-zf0iqh{display:-webkit-box;display:-webkit-flex;display:-ms-flexbox;display:flex;-webkit-flex-direction:column;-ms-flex-direction:column;flex-direction:column;min-height:100vh;}</style><div class="theme-undefined MuiBox-root css-zf0iqh"><style data-emotion="css 1j492j9">.css-1j492j9{z-index:1000;position:fixed;left:0;top:0;width:100%;}</style><div class="MuiBox-root css-1j492j9"><style data-emotion="css 1pokp7a">.css-1pokp7a{height:50px;padding:.6em 0;background:#1d2125;color:white;}</style><div class="MuiBox-root css-1pokp7a"><style data-emotion="css 1ekb41w">.css-1ekb41w{width:100%;margin-left:auto;box-sizing:border-box;margin-right:auto;display:block;padding-left:16px;padding-right:16px;}@media (min-width:600px){.css-1ekb41w{padding-left:24px;padding-right:24px;}}@media (min-width:1536px){.css-1ekb41w{max-width:1536px;}}</style><div class="MuiContainer-root MuiContainer-maxWidthXl css-1ekb41w"><style data-emotion="css k008qs">.css-k008qs{display:-webkit-box;display:-webkit-flex;display:-ms-flexbox;display:flex;}</style><div class="MuiBox-root css-k008qs"><style data-emotion="css zfo34s">.css-zfo34s{display:-webkit-box;display:-webkit-flex;display:-ms-flexbox;display:flex;margin-right:.5em;}@media (max-width:300px){.css-zfo34s{;}}</style><div class="MuiBox-root css-zfo34s"><a href="/"><style data-emotion="css 8cifhi">.css-8cifhi{display:-webkit-box;display:-webkit-flex;display:-ms-flexbox;display:flex;-webkit-align-items:center;-webkit-box-align:center;-ms-flex-align:center;align-items:center;height:100%;cursor:pointer;}</style><div class="MuiBox-root css-8cifhi"><svg xmlns="http://www.w3.org/2000/svg" viewbox="0 0 255.85 77.92" style="height:20px"><path d="M92.2 62.36L91.99.1h7.5q0 8.37 0 16.74a27.92 27.92 0 0 1 12.53-2.57 15.8 15.8 0 0 1 8 2 13.64 13.64 0 0 1 3.1 2.55q4.15 4.59 4.16 12.51v13.5q0 7.83-4.16 12.42a15.44 15.44 0 0 1-3.09 2.55c-4 2.51-7.79 2.42-16.16 2.47-2.42-.03-6.46 0-11.67.09zm17.3-7.07a10 10 0 0 0 7.5-2.83c1.83-1.89 2.75-4.62 2.75-8.17V31.83q0-5.34-2.75-8.17a10 10 0 0 0-7.5-2.83 9.5 9.5 0 0 0-7.3 2.91c-1.8 1.95-2.71 4.65-2.71 8.09v12.51c0 3.44.91 6.14 2.71 8.08a9.47 9.47 0 0 0 7.3 2.87zm27.02 7.07V16.51h7.34a45 45 0 0 1 12.08-.83c5.16.34 7.48 1.41 9 2.41a15 15 0 0 1 2.54 2.13q4.29 4.55 4.29 12.3v2.75h-7.44v-2.09q0-5.41-2.63-8.29c-1.75-1.92-4.23-2.88-7.46-2.88a9.67 9.67 0 0 0-7.46 2.92c-1.81 1.94-2.71 4.7-2.71 8.25v29.18zm39.65 15.57v-6.92h10.76q5.16 0 8-2.8c1.92-1.86 2.88-4.49 2.88-7.87V23.9h-19.14v-6.83h26.66v43.27q0 8.09-5 12.83-5 4.74-13.4 4.76zm24.41-67.25a5.31 5.31 0 0 1-3.9-1.42 5.19 5.19 0 0 1-1.42-3.84 5.49 5.49 0 0 1 1.42-4 5.93 5.93 0 0 1 7.79 0 5.44 5.44 0 0 1 1.42 4 5.16 5.16 0 0 1-1.42 3.84 5.28 5.28 0 0 1-3.89 1.42zM34.45 62.1L0 43.83V39.1l34.45-18.27v4.89L10.33 38.25l-3.5 1.72-2.78 1.39 2.86 1.34 3.42 1.76 24.09 12.65z" fill="#fff"/><path d="M49.95 62.27L72.44.2h4L53.95 62.27z" fill="#e95736"/><path d="M221.39 57.11l24.09-12.65 3.43-1.76 2.86-1.34-2.78-1.39-3.51-1.72-24.09-12.58v-4.84l34.46 18.29v4.71l-34.46 18.3z" fill="#fff"/></svg></div></a></div><style data-emotion="css n7sve0">.css-n7sve0{display:-webkit-box;display:-webkit-flex;display:-ms-flexbox;display:flex;-webkit-align-items:center;-webkit-box-align:center;-ms-flex-align:center;align-items:center;font-size:14pt;-webkit-flex-shrink:0;-ms-flex-negative:0;flex-shrink:0;padding:0 1em;}</style><div class="MuiBox-root css-n7sve0">PHP Manual</div><style data-emotion="css 1olnj5l">.css-1olnj5l{display:-webkit-box;display:-webkit-flex;display:-ms-flexbox;display:flex;-webkit-align-items:center;-webkit-box-align:center;-ms-flex-align:center;align-items:center;-webkit-box-pack:left;-ms-flex-pack:left;-webkit-justify-content:left;justify-content:left;width:100%;}</style><div class="MuiBox-root css-1olnj5l"><style data-emotion="css jogunm">@media (max-width:700px){.css-jogunm{;}}</style><div class="MuiBox-root css-jogunm"><a style="text-decoration:none" href="/"><style data-emotion="css tog0kf">.css-tog0kf{display:-webkit-inline-box;display:-webkit-inline-flex;display:-ms-inline-flexbox;display:inline-flex;-webkit-align-items:center;-webkit-box-align:center;-ms-flex-align:center;align-items:center;-webkit-box-pack:center;-ms-flex-pack:center;-webkit-justify-content:center;justify-content:center;position:relative;box-sizing:border-box;-webkit-tap-highlight-color:transparent;background-color:transparent;outline:0;border:0;margin:0;border-radius:0;padding:0;cursor:pointer;-webkit-user-select:none;-moz-user-select:none;-ms-user-select:none;user-select:none;vertical-align:middle;-moz-appearance:none;-webkit-appearance:none;-webkit-text-decoration:none;text-decoration:none;color:inherit;-webkit-text-decoration:none;text-decoration:none;-webkit-box-pack:left;-ms-flex-pack:left;-webkit-justify-content:left;justify-content:left;color:white;}.css-tog0kf::-moz-focus-inner{border-style:none;}.css-tog0kf.Mui-disabled{pointer-events:none;cursor:default;}@media print{.css-tog0kf{-webkit-print-color-adjust:exact;color-adjust:exact;}}@media (max-width:700px){.css-tog0kf{width:100%;}}@media (min-width:700px){.css-tog0kf{margin:0 .5em;}}</style><span class="MuiButtonBase-root css-tog0kf" tabindex="0" role="button"><style data-emotion="css 1jx1bww">.css-1jx1bww{display:-webkit-box;display:-webkit-flex;display:-ms-flexbox;display:flex;}@media (min-width:700px){.css-1jx1bww{padding:.25em;border-bottom:2px solid transparent;}.css-1jx1bww:hover{border-bottom:2px solid #fff;}}@media (max-width:700px){.css-1jx1bww{padding:0 .75em;}}</style><div class="MuiBox-root css-1jx1bww"><div class="MuiBox-root css-0">Homepage</div></div></span></a><a style="text-decoration:none" href="/zacatek"><span class="MuiButtonBase-root css-tog0kf" tabindex="0" role="button"><div class="MuiBox-root css-1jx1bww"><div class="MuiBox-root css-0">ZAČÁTKY</div></div></span></a><a style="text-decoration:none" href="/navody.php"><span class="MuiButtonBase-root css-tog0kf" tabindex="0" role="button"><div class="MuiBox-root css-1jx1bww"><div class="MuiBox-root css-0">NÁVODY</div></div></span></a><a style="text-decoration:none" href="/objektove-orientovane-programovani-v-php.php"><span class="MuiButtonBase-root css-tog0kf" tabindex="0" role="button"><div class="MuiBox-root css-1jx1bww"><div class="MuiBox-root css-0">OOP</div></div></span></a><a style="text-decoration:none" href="/dokumentace.php"><span class="MuiButtonBase-root css-tog0kf" tabindex="0" role="button"><div class="MuiBox-root css-1jx1bww"><div class="MuiBox-root css-0">DOKUMENTACE</div></div></span></a></div></div><style data-emotion="css 19t2ho9">.css-19t2ho9{display:-webkit-box;display:-webkit-flex;display:-ms-flexbox;display:flex;-webkit-align-items:stretch;-webkit-box-align:stretch;-ms-flex-align:stretch;align-items:stretch;}</style><div class="MuiBox-root css-19t2ho9"><style data-emotion="css 1eto8wm">.css-1eto8wm{text-align:center;-webkit-flex:0 0 auto;-ms-flex:0 0 auto;flex:0 0 auto;font-size:1.5rem;padding:8px;border-radius:50%;overflow:visible;color:rgba(0, 0, 0, 0.54);-webkit-transition:background-color 150ms cubic-bezier(0.4, 0, 0.2, 1) 0ms;transition:background-color 150ms cubic-bezier(0.4, 0, 0.2, 1) 0ms;padding:.15em;margin-right:8px;}.css-1eto8wm:hover{background-color:rgba(0, 0, 0, 0.04);}@media (hover: none){.css-1eto8wm:hover{background-color:transparent;}}.css-1eto8wm.Mui-disabled{background-color:transparent;color:rgba(0, 0, 0, 0.26);}</style><style data-emotion="css 1yegd7v">.css-1yegd7v{display:-webkit-inline-box;display:-webkit-inline-flex;display:-ms-inline-flexbox;display:inline-flex;-webkit-align-items:center;-webkit-box-align:center;-ms-flex-align:center;align-items:center;-webkit-box-pack:center;-ms-flex-pack:center;-webkit-justify-content:center;justify-content:center;position:relative;box-sizing:border-box;-webkit-tap-highlight-color:transparent;background-color:transparent;outline:0;border:0;margin:0;border-radius:0;padding:0;cursor:pointer;-webkit-user-select:none;-moz-user-select:none;-ms-user-select:none;user-select:none;vertical-align:middle;-moz-appearance:none;-webkit-appearance:none;-webkit-text-decoration:none;text-decoration:none;color:inherit;text-align:center;-webkit-flex:0 0 auto;-ms-flex:0 0 auto;flex:0 0 auto;font-size:1.5rem;padding:8px;border-radius:50%;overflow:visible;color:rgba(0, 0, 0, 0.54);-webkit-transition:background-color 150ms cubic-bezier(0.4, 0, 0.2, 1) 0ms;transition:background-color 150ms cubic-bezier(0.4, 0, 0.2, 1) 0ms;padding:.15em;margin-right:8px;}.css-1yegd7v::-moz-focus-inner{border-style:none;}.css-1yegd7v.Mui-disabled{pointer-events:none;cursor:default;}@media print{.css-1yegd7v{-webkit-print-color-adjust:exact;color-adjust:exact;}}.css-1yegd7v:hover{background-color:rgba(0, 0, 0, 0.04);}@media (hover: none){.css-1yegd7v:hover{background-color:transparent;}}.css-1yegd7v.Mui-disabled{background-color:transparent;color:rgba(0, 0, 0, 0.26);}</style><button class="MuiButtonBase-root MuiIconButton-root MuiIconButton-sizeMedium css-1yegd7v" tabindex="0" type="button"><style data-emotion="css 675ts4">.css-675ts4{-webkit-user-select:none;-moz-user-select:none;-ms-user-select:none;user-select:none;width:1em;height:1em;display:inline-block;fill:currentColor;-webkit-flex-shrink:0;-ms-flex-negative:0;flex-shrink:0;-webkit-transition:fill 200ms cubic-bezier(0.4, 0, 0.2, 1) 0ms;transition:fill 200ms cubic-bezier(0.4, 0, 0.2, 1) 0ms;font-size:1.5rem;color:#fff;}</style><svg class="MuiSvgIcon-root MuiSvgIcon-fontSizeMedium css-675ts4" focusable="false" aria-hidden="true" viewbox="0 0 24 24" data-testid="SearchIcon"><path d="M15.5 14h-.79l-.28-.27C15.41 12.59 16 11.11 16 9.5 16 5.91 13.09 3 9.5 3S3 5.91 3 9.5 5.91 16 9.5 16c1.61 0 3.09-.59 4.23-1.57l.27.28v.79l5 4.99L20.49 19l-4.99-5zm-6 0C7.01 14 5 11.99 5 9.5S7.01 5 9.5 5 14 7.01 14 9.5 11.99 14 9.5 14z"/></svg></button></div><style data-emotion="css 1v8pci6">.css-1v8pci6{text-align:center;-webkit-flex:0 0 auto;-ms-flex:0 0 auto;flex:0 0 auto;font-size:1.5rem;padding:8px;border-radius:50%;overflow:visible;color:rgba(0, 0, 0, 0.54);-webkit-transition:background-color 150ms cubic-bezier(0.4, 0, 0.2, 1) 0ms;transition:background-color 150ms cubic-bezier(0.4, 0, 0.2, 1) 0ms;padding:.15em;margin-left:8px;margin-right:8px;}.css-1v8pci6:hover{background-color:rgba(0, 0, 0, 0.04);}@media (hover: none){.css-1v8pci6:hover{background-color:transparent;}}.css-1v8pci6.Mui-disabled{background-color:transparent;color:rgba(0, 0, 0, 0.26);}@media (min-width:701px){.css-1v8pci6{;}}</style><style data-emotion="css zpuxhy">.css-zpuxhy{display:-webkit-inline-box;display:-webkit-inline-flex;display:-ms-inline-flexbox;display:inline-flex;-webkit-align-items:center;-webkit-box-align:center;-ms-flex-align:center;align-items:center;-webkit-box-pack:center;-ms-flex-pack:center;-webkit-justify-content:center;justify-content:center;position:relative;box-sizing:border-box;-webkit-tap-highlight-color:transparent;background-color:transparent;outline:0;border:0;margin:0;border-radius:0;padding:0;cursor:pointer;-webkit-user-select:none;-moz-user-select:none;-ms-user-select:none;user-select:none;vertical-align:middle;-moz-appearance:none;-webkit-appearance:none;-webkit-text-decoration:none;text-decoration:none;color:inherit;text-align:center;-webkit-flex:0 0 auto;-ms-flex:0 0 auto;flex:0 0 auto;font-size:1.5rem;padding:8px;border-radius:50%;overflow:visible;color:rgba(0, 0, 0, 0.54);-webkit-transition:background-color 150ms cubic-bezier(0.4, 0, 0.2, 1) 0ms;transition:background-color 150ms cubic-bezier(0.4, 0, 0.2, 1) 0ms;padding:.15em;margin-left:8px;margin-right:8px;}.css-zpuxhy::-moz-focus-inner{border-style:none;}.css-zpuxhy.Mui-disabled{pointer-events:none;cursor:default;}@media print{.css-zpuxhy{-webkit-print-color-adjust:exact;color-adjust:exact;}}.css-zpuxhy:hover{background-color:rgba(0, 0, 0, 0.04);}@media (hover: none){.css-zpuxhy:hover{background-color:transparent;}}.css-zpuxhy.Mui-disabled{background-color:transparent;color:rgba(0, 0, 0, 0.26);}@media (min-width:701px){.css-zpuxhy{;}}</style><button class="MuiButtonBase-root MuiIconButton-root MuiIconButton-sizeMedium css-zpuxhy" tabindex="0" type="button"><svg class="MuiSvgIcon-root MuiSvgIcon-fontSizeMedium css-675ts4" focusable="false" aria-hidden="true" viewbox="0 0 24 24" data-testid="MenuIcon"><path d="M3 18h18v-2H3v2zm0-5h18v-2H3v2zm0-7v2h18V6H3z"/></svg></button><style data-emotion="css 1dkn3rg">.css-1dkn3rg{text-align:center;-webkit-flex:0 0 auto;-ms-flex:0 0 auto;flex:0 0 auto;font-size:1.5rem;padding:8px;border-radius:50%;overflow:visible;color:rgba(0, 0, 0, 0.54);-webkit-transition:background-color 150ms cubic-bezier(0.4, 0, 0.2, 1) 0ms;transition:background-color 150ms cubic-bezier(0.4, 0, 0.2, 1) 0ms;padding:.15em;margin-left:8px;margin-right:8px;}.css-1dkn3rg:hover{background-color:rgba(0, 0, 0, 0.04);}@media (hover: none){.css-1dkn3rg:hover{background-color:transparent;}}.css-1dkn3rg.Mui-disabled{background-color:transparent;color:rgba(0, 0, 0, 0.26);}@media (max-width:701px){.css-1dkn3rg{;}}</style><style data-emotion="css yn0l94">.css-yn0l94{display:-webkit-inline-box;display:-webkit-inline-flex;display:-ms-inline-flexbox;display:inline-flex;-webkit-align-items:center;-webkit-box-align:center;-ms-flex-align:center;align-items:center;-webkit-box-pack:center;-ms-flex-pack:center;-webkit-justify-content:center;justify-content:center;position:relative;box-sizing:border-box;-webkit-tap-highlight-color:transparent;background-color:transparent;outline:0;border:0;margin:0;border-radius:0;padding:0;cursor:pointer;-webkit-user-select:none;-moz-user-select:none;-ms-user-select:none;user-select:none;vertical-align:middle;-moz-appearance:none;-webkit-appearance:none;-webkit-text-decoration:none;text-decoration:none;color:inherit;text-align:center;-webkit-flex:0 0 auto;-ms-flex:0 0 auto;flex:0 0 auto;font-size:1.5rem;padding:8px;border-radius:50%;overflow:visible;color:rgba(0, 0, 0, 0.54);-webkit-transition:background-color 150ms cubic-bezier(0.4, 0, 0.2, 1) 0ms;transition:background-color 150ms cubic-bezier(0.4, 0, 0.2, 1) 0ms;padding:.15em;margin-left:8px;margin-right:8px;}.css-yn0l94::-moz-focus-inner{border-style:none;}.css-yn0l94.Mui-disabled{pointer-events:none;cursor:default;}@media print{.css-yn0l94{-webkit-print-color-adjust:exact;color-adjust:exact;}}.css-yn0l94:hover{background-color:rgba(0, 0, 0, 0.04);}@media (hover: none){.css-yn0l94:hover{background-color:transparent;}}.css-yn0l94.Mui-disabled{background-color:transparent;color:rgba(0, 0, 0, 0.26);}@media (max-width:701px){.css-yn0l94{;}}</style><button class="MuiButtonBase-root MuiIconButton-root MuiIconButton-sizeMedium css-yn0l94" tabindex="0" type="button"><style data-emotion="css 7r06y4">.css-7r06y4{-webkit-user-select:none;-moz-user-select:none;-ms-user-select:none;user-select:none;width:1em;height:1em;display:inline-block;fill:currentColor;-webkit-flex-shrink:0;-ms-flex-negative:0;flex-shrink:0;-webkit-transition:fill 200ms cubic-bezier(0.4, 0, 0.2, 1) 0ms;transition:fill 200ms cubic-bezier(0.4, 0, 0.2, 1) 0ms;font-size:1.5rem;color:#FFEB3B;}</style><svg class="MuiSvgIcon-root MuiSvgIcon-fontSizeMedium css-7r06y4" focusable="false" aria-hidden="true" viewbox="0 0 24 24" data-testid="LightModeIcon"><path d="M12 7c-2.76 0-5 2.24-5 5s2.24 5 5 5 5-2.24 5-5-2.24-5-5-5zM2 13h2c.55 0 1-.45 1-1s-.45-1-1-1H2c-.55 0-1 .45-1 1s.45 1 1 1zm18 0h2c.55 0 1-.45 1-1s-.45-1-1-1h-2c-.55 0-1 .45-1 1s.45 1 1 1zM11 2v2c0 .55.45 1 1 1s1-.45 1-1V2c0-.55-.45-1-1-1s-1 .45-1 1zm0 18v2c0 .55.45 1 1 1s1-.45 1-1v-2c0-.55-.45-1-1-1s-1 .45-1 1zM5.99 4.58c-.39-.39-1.03-.39-1.41 0-.39.39-.39 1.03 0 1.41l1.06 1.06c.39.39 1.03.39 1.41 0s.39-1.03 0-1.41L5.99 4.58zm12.37 12.37c-.39-.39-1.03-.39-1.41 0-.39.39-.39 1.03 0 1.41l1.06 1.06c.39.39 1.03.39 1.41 0 .39-.39.39-1.03 0-1.41l-1.06-1.06zm1.06-10.96c.39-.39.39-1.03 0-1.41-.39-.39-1.03-.39-1.41 0l-1.06 1.06c-.39.39-.39 1.03 0 1.41s1.03.39 1.41 0l1.06-1.06zM7.05 18.36c.39-.39.39-1.03 0-1.41-.39-.39-1.03-.39-1.41 0l-1.06 1.06c-.39.39-.39 1.03 0 1.41s1.03.39 1.41 0l1.06-1.06z"/></svg></button></div></div><style data-emotion="css 12ricle">.css-12ricle{left:0;top:0;width:100%;z-index:9999;}@media (min-width:701px){.css-12ricle{;}}</style><div class="MuiBox-root css-12ricle"><style data-emotion="css a0y2e3">.css-a0y2e3{height:0;overflow:hidden;-webkit-transition:height 300ms cubic-bezier(0.4, 0, 0.2, 1) 0ms;transition:height 300ms cubic-bezier(0.4, 0, 0.2, 1) 0ms;;}</style><div class="MuiCollapse-root MuiCollapse-vertical MuiCollapse-hidden css-a0y2e3" style="min-height:0px"><style data-emotion="css hboir5">.css-hboir5{display:-webkit-box;display:-webkit-flex;display:-ms-flexbox;display:flex;width:100%;}</style><div class="MuiCollapse-wrapper MuiCollapse-vertical css-hboir5"><style data-emotion="css 8atqhb">.css-8atqhb{width:100%;}</style><div class="MuiCollapse-wrapperInner MuiCollapse-vertical css-8atqhb"><style data-emotion="css 1c6wyi6">.css-1c6wyi6{border:1px solid #30363e;border-radius:.25rem;margin:.75em 0;background:#0d1117;}</style><div class="MuiBox-root css-1c6wyi6"><style data-emotion="css old1by">.css-old1by{padding:0px;}</style><div class="MuiBox-root css-old1by"><style data-emotion="css irgx2y">.css-irgx2y{text-align:right;padding-left:16px;padding-right:16px;padding-top:8px;}</style><div class="MuiBox-root css-irgx2y"><style data-emotion="css 15agh50">.css-15agh50{text-align:center;-webkit-flex:0 0 auto;-ms-flex:0 0 auto;flex:0 0 auto;font-size:1.5rem;padding:8px;border-radius:50%;overflow:visible;color:rgba(0, 0, 0, 0.54);-webkit-transition:background-color 150ms cubic-bezier(0.4, 0, 0.2, 1) 0ms;transition:background-color 150ms cubic-bezier(0.4, 0, 0.2, 1) 0ms;padding:.15em;}.css-15agh50:hover{background-color:rgba(0, 0, 0, 0.04);}@media (hover: none){.css-15agh50:hover{background-color:transparent;}}.css-15agh50.Mui-disabled{background-color:transparent;color:rgba(0, 0, 0, 0.26);}</style><style data-emotion="css 1lgg6e5">.css-1lgg6e5{display:-webkit-inline-box;display:-webkit-inline-flex;display:-ms-inline-flexbox;display:inline-flex;-webkit-align-items:center;-webkit-box-align:center;-ms-flex-align:center;align-items:center;-webkit-box-pack:center;-ms-flex-pack:center;-webkit-justify-content:center;justify-content:center;position:relative;box-sizing:border-box;-webkit-tap-highlight-color:transparent;background-color:transparent;outline:0;border:0;margin:0;border-radius:0;padding:0;cursor:pointer;-webkit-user-select:none;-moz-user-select:none;-ms-user-select:none;user-select:none;vertical-align:middle;-moz-appearance:none;-webkit-appearance:none;-webkit-text-decoration:none;text-decoration:none;color:inherit;text-align:center;-webkit-flex:0 0 auto;-ms-flex:0 0 auto;flex:0 0 auto;font-size:1.5rem;padding:8px;border-radius:50%;overflow:visible;color:rgba(0, 0, 0, 0.54);-webkit-transition:background-color 150ms cubic-bezier(0.4, 0, 0.2, 1) 0ms;transition:background-color 150ms cubic-bezier(0.4, 0, 0.2, 1) 0ms;padding:.15em;}.css-1lgg6e5::-moz-focus-inner{border-style:none;}.css-1lgg6e5.Mui-disabled{pointer-events:none;cursor:default;}@media print{.css-1lgg6e5{-webkit-print-color-adjust:exact;color-adjust:exact;}}.css-1lgg6e5:hover{background-color:rgba(0, 0, 0, 0.04);}@media (hover: none){.css-1lgg6e5:hover{background-color:transparent;}}.css-1lgg6e5.Mui-disabled{background-color:transparent;color:rgba(0, 0, 0, 0.26);}</style><button class="MuiButtonBase-root MuiIconButton-root MuiIconButton-sizeMedium css-1lgg6e5" tabindex="0" type="button"><svg class="MuiSvgIcon-root MuiSvgIcon-fontSizeMedium css-675ts4" focusable="false" aria-hidden="true" viewbox="0 0 24 24" data-testid="CloseIcon"><path d="M19 6.41 17.59 5 12 10.59 6.41 5 5 6.41 10.59 12 5 17.59 6.41 19 12 13.41 17.59 19 19 17.59 13.41 12z"/></svg></button></div><style data-emotion="css 1ebprri">.css-1ebprri{margin-top:16px;margin-bottom:16px;}</style><div class="MuiBox-root css-1ebprri"><a style="text-decoration:none" href="/"><span class="MuiButtonBase-root css-tog0kf" tabindex="0" role="button"><div class="MuiBox-root css-1jx1bww"><div class="MuiBox-root css-0">Homepage</div></div></span></a></div><div class="MuiBox-root css-1ebprri"><a style="text-decoration:none" href="/zacatek"><span class="MuiButtonBase-root css-tog0kf" tabindex="0" role="button"><div class="MuiBox-root css-1jx1bww"><div class="MuiBox-root css-0">ZAČÁTKY</div></div></span></a></div><div class="MuiBox-root css-1ebprri"><a style="text-decoration:none" href="/navody.php"><span class="MuiButtonBase-root css-tog0kf" tabindex="0" role="button"><div class="MuiBox-root css-1jx1bww"><div class="MuiBox-root css-0">NÁVODY</div></div></span></a></div><div class="MuiBox-root css-1ebprri"><a style="text-decoration:none" href="/objektove-orientovane-programovani-v-php.php"><span class="MuiButtonBase-root css-tog0kf" tabindex="0" role="button"><div class="MuiBox-root css-1jx1bww"><div class="MuiBox-root css-0">OOP</div></div></span></a></div><div class="MuiBox-root css-1ebprri"><a style="text-decoration:none" href="/dokumentace.php"><span class="MuiButtonBase-root css-tog0kf" tabindex="0" role="button"><div class="MuiBox-root css-1jx1bww"><div class="MuiBox-root css-0">DOKUMENTACE</div></div></span></a></div></div></div></div></div></div></div></div></div><style data-emotion="css jn01mi">.css-jn01mi{margin-top:50px;}</style><div class="MuiBox-root css-jn01mi"><style data-emotion="css dgk07y">.css-dgk07y{border:1px solid #30363e;border-radius:.25rem;margin:0px;background:#0d1117;}</style><div class="MuiBox-root css-dgk07y"><style data-emotion="css 5myvww">.css-5myvww{display:-webkit-box;display:-webkit-flex;display:-ms-flexbox;display:flex;padding:.25em .75em;border-top-left-radius:.25rem;border-top-right-radius:.25rem;background:#161b22;}</style><div class="MuiBox-root css-5myvww"><div class="MuiBox-root css-8atqhb"><div class="MuiBox-root css-k008qs"><style data-emotion="css 7pf6at">.css-7pf6at{display:-webkit-box;display:-webkit-flex;display:-ms-flexbox;display:flex;-webkit-align-items:center;-webkit-box-align:center;-ms-flex-align:center;align-items:center;width:100%;}</style><div class="MuiBox-root css-7pf6at"><div class="MuiBox-root css-k008qs"><div class="MuiBox-root css-0"><a href="/jiao-cheng">教程</a></div><style data-emotion="css o4olfn">.css-o4olfn{margin:0 .5em;color:#aaa;}</style><span class="MuiBox-root css-o4olfn">/</span></div><b><a href="/ji-ben-zhi-shi">基本知识</a></b></div><style data-emotion="css 1o9g0p">.css-1o9g0p{width:180px;}</style><div class="MuiBox-root css-1o9g0p"><style data-emotion="css y9jh7n">.css-y9jh7n{display:-webkit-inline-box;display:-webkit-inline-flex;display:-ms-inline-flexbox;display:inline-flex;-webkit-align-items:center;-webkit-box-align:center;-ms-flex-align:center;align-items:center;-webkit-box-pack:center;-ms-flex-pack:center;-webkit-justify-content:center;justify-content:center;position:relative;box-sizing:border-box;-webkit-tap-highlight-color:transparent;background-color:transparent;outline:0;border:0;margin:0;border-radius:0;padding:0;cursor:pointer;-webkit-user-select:none;-moz-user-select:none;-ms-user-select:none;user-select:none;vertical-align:middle;-moz-appearance:none;-webkit-appearance:none;-webkit-text-decoration:none;text-decoration:none;color:inherit;border:1px solid rgba(0, 0, 0, .125);border-radius:6px;padding:.25em;width:100%;}.css-y9jh7n::-moz-focus-inner{border-style:none;}.css-y9jh7n.Mui-disabled{pointer-events:none;cursor:default;}@media print{.css-y9jh7n{-webkit-print-color-adjust:exact;color-adjust:exact;}}.css-y9jh7n:hover{background:#f8fafb;color:black;}</style><button class="MuiButtonBase-root css-y9jh7n" tabindex="0" type="button"><style data-emotion="css 19nenu6">.css-19nenu6{-webkit-user-select:none;-moz-user-select:none;-ms-user-select:none;user-select:none;width:1em;height:1em;display:inline-block;fill:currentColor;-webkit-flex-shrink:0;-ms-flex-negative:0;flex-shrink:0;-webkit-transition:fill 200ms cubic-bezier(0.4, 0, 0.2, 1) 0ms;transition:fill 200ms cubic-bezier(0.4, 0, 0.2, 1) 0ms;font-size:1.5rem;font-size:12pt;margin-right:8px;}</style><svg class="MuiSvgIcon-root MuiSvgIcon-fontSizeMedium css-19nenu6" focusable="false" aria-hidden="true" viewbox="0 0 24 24" data-testid="AutoFixHighIcon"><path d="M7.5 5.6 10 7 8.6 4.5 10 2 7.5 3.4 5 2l1.4 2.5L5 7zm12 9.8L17 14l1.4 2.5L17 19l2.5-1.4L22 19l-1.4-2.5L22 14zM22 2l-2.5 1.4L17 2l1.4 2.5L17 7l2.5-1.4L22 7l-1.4-2.5zm-7.63 5.29a.9959.9959 0 0 0-1.41 0L1.29 18.96c-.39.39-.39 1.02 0 1.41l2.34 2.34c.39.39 1.02.39 1.41 0L16.7 11.05c.39-.39.39-1.02 0-1.41l-2.33-2.35zm-1.03 5.49-2.12-2.12 2.44-2.44 2.12 2.12-2.44 2.44z"/></svg>Navrhnout změnu</button><style data-emotion="css 1k371a6">@media print{.css-1k371a6{position:absolute!important;}}</style></div></div></div></div><div class="MuiBox-root css-old1by"/></div><style data-emotion="css 1qsxih2">.css-1qsxih2{width:100%;margin-left:auto;box-sizing:border-box;margin-right:auto;display:block;padding-left:16px;padding-right:16px;}@media (min-width:600px){.css-1qsxih2{padding-left:24px;padding-right:24px;}}@media (min-width:1200px){.css-1qsxih2{max-width:1200px;}}</style><div class="MuiContainer-root MuiContainer-maxWidthLg css-1qsxih2"><style data-emotion="css 2imjyh">.css-2imjyh{display:-webkit-box;display:-webkit-flex;display:-ms-flexbox;display:flex;-webkit-box-flex-wrap:wrap;-webkit-flex-wrap:wrap;-ms-flex-wrap:wrap;flex-wrap:wrap;}</style><div class="MuiBox-root css-2imjyh"><style data-emotion="css 1nlp836">.css-1nlp836{width:70%;padding:1em;}@media (max-width:780px){.css-1nlp836{width:100%;}}</style><div class="MuiBox-root css-1nlp836"><h1>PHP中的循环和它们的类型</h1><div class="MuiBox-root css-k008qs"><style data-emotion="css qiye2o">.css-qiye2o{display:-webkit-box;display:-webkit-flex;display:-ms-flexbox;display:flex;width:50%;}</style><div class="MuiBox-root css-qiye2o"><style data-emotion="css 6hgbv2">.css-6hgbv2{-webkit-align-items:center;-webkit-box-align:center;-ms-flex-align:center;align-items:center;padding-right:8px;}</style><div class="MuiBox-root css-6hgbv2"><style data-emotion="css vubbuv">.css-vubbuv{-webkit-user-select:none;-moz-user-select:none;-ms-user-select:none;user-select:none;width:1em;height:1em;display:inline-block;fill:currentColor;-webkit-flex-shrink:0;-ms-flex-negative:0;flex-shrink:0;-webkit-transition:fill 200ms cubic-bezier(0.4, 0, 0.2, 1) 0ms;transition:fill 200ms cubic-bezier(0.4, 0, 0.2, 1) 0ms;font-size:1.5rem;}</style><svg class="MuiSvgIcon-root MuiSvgIcon-fontSizeMedium css-vubbuv" focusable="false" aria-hidden="true" viewbox="0 0 24 24" data-testid="CalendarMonthIcon"><path d="M19 4h-1V2h-2v2H8V2H6v2H5c-1.11 0-1.99.9-1.99 2L3 20c0 1.1.89 2 2 2h14c1.1 0 2-.9 2-2V6c0-1.1-.9-2-2-2zm0 16H5V10h14v10zM9 14H7v-2h2v2zm4 0h-2v-2h2v2zm4 0h-2v-2h2v2zm-8 4H7v-2h2v2zm4 0h-2v-2h2v2zm4 0h-2v-2h2v2z"/></svg></div><style data-emotion="css zcxndt">.css-zcxndt{-webkit-align-items:center;-webkit-box-align:center;-ms-flex-align:center;align-items:center;}</style><div class="MuiBox-root css-zcxndt">11. 04. 2020</div></div><style data-emotion="css 1gkhuld">.css-1gkhuld{display:-webkit-box;display:-webkit-flex;display:-ms-flexbox;display:flex;width:50%;-webkit-box-pack:right;-ms-flex-pack:right;-webkit-justify-content:right;justify-content:right;}</style><div class="MuiBox-root css-1gkhuld"><div class="MuiBox-root css-6hgbv2"><svg class="MuiSvgIcon-root MuiSvgIcon-fontSizeMedium css-vubbuv" focusable="false" aria-hidden="true" viewbox="0 0 24 24" data-testid="AccountCircleIcon"><path d="M12 2C6.48 2 2 6.48 2 12s4.48 10 10 10 10-4.48 10-10S17.52 2 12 2zm0 4c1.93 0 3.5 1.57 3.5 3.5S13.93 13 12 13s-3.5-1.57-3.5-3.5S10.07 6 12 6zm0 14c-2.03 0-4.43-.82-6.14-2.88C7.55 15.8 9.68 15 12 15s4.45.8 6.14 2.12C16.43 19.18 14.03 20 12 20z"/></svg></div><div class="MuiBox-root css-zcxndt"><a href="/janbarasek">Jan Barášek</a></div></div></div><div class="MuiBox-root css-1c6wyi6"><div class="MuiBox-root css-5myvww"><style data-emotion="css 111wug5">.css-111wug5{padding-right:8px;}</style><div class="MuiBox-root css-111wug5"><svg class="MuiSvgIcon-root MuiSvgIcon-fontSizeMedium css-vubbuv" focusable="false" aria-hidden="true" viewbox="0 0 24 24" data-testid="ListIcon"><path d="M3 13h2v-2H3v2zm0 4h2v-2H3v2zm0-8h2V7H3v2zm4 4h14v-2H7v2zm0 4h14v-2H7v2zM7 7v2h14V7H7z"/></svg></div><div class="MuiBox-root css-8atqhb"><h2 style="font-size:1.1rem;margin:0">Obsah článku</h2></div></div><div class="MuiBox-root css-old1by"><style data-emotion="css h8g7xa">.css-h8g7xa{margin:.5em 0;}</style><div class="MuiBox-root css-h8g7xa"><ol style="margin-bottom:0;line-height:1.75"><li><a href="/php-zhong-de-xun-huan-he-ta-men-de-lei-xing#xun-huan-de-yi-ban-shu-xing">循环的一般属性</a></li><li><a href="/php-zhong-de-xun-huan-he-ta-men-de-lei-xing#dui-yu-xun-huan">对于循环</a></li><li><a href="/php-zhong-de-xun-huan-he-ta-men-de-lei-xing#dang-yi-gedo-while-xun-huan">当一个do-while循环</a></li><li><a href="/php-zhong-de-xun-huan-he-ta-men-de-lei-xing#yu-lan-qi">预览器</a></li><li><a href="/php-zhong-de-xun-huan-he-ta-men-de-lei-xing#di-gui">递归</a></li></ol></div><style data-emotion="css zgnaae">.css-zgnaae{display:-webkit-inline-box;display:-webkit-inline-flex;display:-ms-inline-flexbox;display:inline-flex;-webkit-align-items:center;-webkit-box-align:center;-ms-flex-align:center;align-items:center;-webkit-box-pack:center;-ms-flex-pack:center;-webkit-justify-content:center;justify-content:center;position:relative;box-sizing:border-box;-webkit-tap-highlight-color:transparent;background-color:transparent;outline:0;border:0;margin:0;border-radius:0;padding:0;cursor:pointer;-webkit-user-select:none;-moz-user-select:none;-ms-user-select:none;user-select:none;vertical-align:middle;-moz-appearance:none;-webkit-appearance:none;-webkit-text-decoration:none;text-decoration:none;color:inherit;width:100%;border-top:1px solid rgba(0, 0, 0, .125);}.css-zgnaae::-moz-focus-inner{border-style:none;}.css-zgnaae.Mui-disabled{pointer-events:none;cursor:default;}@media print{.css-zgnaae{-webkit-print-color-adjust:exact;color-adjust:exact;}}.css-zgnaae:hover{background:#161b22;}</style><button class="MuiButtonBase-root css-zgnaae" tabindex="0" type="button">Zobrazit více<svg class="MuiSvgIcon-root MuiSvgIcon-fontSizeMedium css-vubbuv" focusable="false" aria-hidden="true" viewbox="0 0 24 24" data-testid="KeyboardArrowDownIcon"><path d="M7.41 8.59 12 13.17l4.59-4.58L18 10l-6 6-6-6 1.41-1.41z"/></svg></button></div></div><style data-emotion="css 9l3uo3">.css-9l3uo3{margin:0;font-family:"Roboto","Helvetica","Arial",sans-serif;font-weight:400;font-size:1rem;line-height:1.5;letter-spacing:0.00938em;}</style><p class="MuiTypography-root MuiTypography-body1 markdown css-9l3uo3">循环一般用于重复相同的代码部分(通常是在一个数据集上)对于每一种类型的任务,都有不同类型的循环适合,其主要区别在于含义和语法。还需要注意的是,所有类型的循环都是可以相互转换的,只是在代码的复杂性和计算时间上可能并不总是值得的。 </p><p>循环的基本划分是以元素类型来划分的。</p> <code>for</code>:一个通用的循环,我们事先知道重复的次数,或者可以简单地事先计算出来。</li> <code>while</code>和<code>until-while</code>:一个一般的循环,我们事先不知道重复的次数。</li> <code>foreach</code>:浏览数组和对象。</li> <li>递归":将一个复杂的问题分解成一组较小的问题。</li> <div id="xun-huan-de-yi-ban-shu-xing" class="content-anchor"/><h2>循环的一般属性</h2> <p>循环通常是通过重复一段有标记的代码来实现的,<strong>重复</strong>,<strong>只要终止条件</strong>成立。循环的停止是由于不能满足终止条件,或者通过调用<code>break;</code>手动停止。</p> <p>如果没有任何东西停止循环,那么就不能阻止它无限期地运行,或直到应用程序被手动终止,或处理PHP脚本的时间限制用完(通常是30秒)。</p> <p><strong>重要的术语:</strong>。</p> <li>循环"--一种语言表达方式,允许你重复代码的某一部分。</li> <li>迭代"--循环体的一个具体执行。</li> <li>初始化"--开始执行一个循环(在第一次迭代开始之前)。</li> <li>递增"--将一个迭代变量递增1。</li> <li>退出条件"--在每个迭代中被验证的条件(验证的位置因循环类型而异)。只要条件成立,该循环就会运行</li> <p><code>For</code>对于我们事先知道重复次数(或可以很容易地计算出来)的情况很有用。例如,它非常适合于区间的线性遍历。</p> <p>它一般是这样写的(3部分)。</p> <style data-emotion="css h773az">.css-h773az{margin-bottom:1.5em;}</style><div class="MuiBox-root css-h773az"><style data-emotion="css 1uwjw6e">.css-1uwjw6e{display:-webkit-box;display:-webkit-flex;display:-ms-flexbox;display:flex;-webkit-box-pack:right;-ms-flex-pack:right;-webkit-justify-content:right;justify-content:right;}</style><div class="MuiBox-root css-1uwjw6e"><style data-emotion="css d24fs7">.css-d24fs7{margin:1em .5em;}</style><div class="MuiBox-root css-d24fs7"><style data-emotion="css 1wf493t">.css-1wf493t{text-align:center;-webkit-flex:0 0 auto;-ms-flex:0 0 auto;flex:0 0 auto;font-size:1.5rem;padding:8px;border-radius:50%;overflow:visible;color:rgba(0, 0, 0, 0.54);-webkit-transition:background-color 150ms cubic-bezier(0.4, 0, 0.2, 1) 0ms;transition:background-color 150ms cubic-bezier(0.4, 0, 0.2, 1) 0ms;}.css-1wf493t:hover{background-color:rgba(0, 0, 0, 0.04);}@media (hover: none){.css-1wf493t:hover{background-color:transparent;}}.css-1wf493t.Mui-disabled{background-color:transparent;color:rgba(0, 0, 0, 0.26);}</style><style data-emotion="css 1yxmbwk">.css-1yxmbwk{display:-webkit-inline-box;display:-webkit-inline-flex;display:-ms-inline-flexbox;display:inline-flex;-webkit-align-items:center;-webkit-box-align:center;-ms-flex-align:center;align-items:center;-webkit-box-pack:center;-ms-flex-pack:center;-webkit-justify-content:center;justify-content:center;position:relative;box-sizing:border-box;-webkit-tap-highlight-color:transparent;background-color:transparent;outline:0;border:0;margin:0;border-radius:0;padding:0;cursor:pointer;-webkit-user-select:none;-moz-user-select:none;-ms-user-select:none;user-select:none;vertical-align:middle;-moz-appearance:none;-webkit-appearance:none;-webkit-text-decoration:none;text-decoration:none;color:inherit;text-align:center;-webkit-flex:0 0 auto;-ms-flex:0 0 auto;flex:0 0 auto;font-size:1.5rem;padding:8px;border-radius:50%;overflow:visible;color:rgba(0, 0, 0, 0.54);-webkit-transition:background-color 150ms cubic-bezier(0.4, 0, 0.2, 1) 0ms;transition:background-color 150ms cubic-bezier(0.4, 0, 0.2, 1) 0ms;}.css-1yxmbwk::-moz-focus-inner{border-style:none;}.css-1yxmbwk.Mui-disabled{pointer-events:none;cursor:default;}@media print{.css-1yxmbwk{-webkit-print-color-adjust:exact;color-adjust:exact;}}.css-1yxmbwk:hover{background-color:rgba(0, 0, 0, 0.04);}@media (hover: none){.css-1yxmbwk:hover{background-color:transparent;}}.css-1yxmbwk.Mui-disabled{background-color:transparent;color:rgba(0, 0, 0, 0.26);}</style><button class="MuiButtonBase-root MuiIconButton-root MuiIconButton-sizeMedium css-1yxmbwk" tabindex="0" type="button"><svg class="MuiSvgIcon-root MuiSvgIcon-fontSizeMedium css-vubbuv" focusable="false" aria-hidden="true" viewbox="0 0 24 24" data-testid="ContentCopyIcon"><path d="M16 1H4c-1.1 0-2 .9-2 2v14h2V3h12V1zm3 4H8c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h11c1.1 0 2-.9 2-2V7c0-1.1-.9-2-2-2zm0 16H8V7h11v14z"/></svg></button></div></div><pre class="prism-code language-php" style="background-color:#2a2734;color:#9a86fd;overflow:auto"><div class="token-line" style="color:#9a86fd"><span class="token plain">for (inicializace; výraz; inkrementace) {</span></div><div class="token-line" style="color:#9a86fd"><span class="token plain"> // 循环的主体</span></div><div class="token-line" style="color:#9a86fd"><span class="token plain">}</span></div></pre></div><p class="MuiTypography-root MuiTypography-body1 markdown css-9l3uo3"> <code>初始化</code>:定义循环的初始状态(我们需要哪些变量)。</li> <li>`表达式': 条件。只要它是有效的,该循环就会执行。</li> <code>increment</code>:改变上一次循环的状态(<code>increment</code>-增量为1)。</li> <p>一个例子是输出一系列从<code>0</code>到<code>10</code>的数字。</p> <div class="MuiBox-root css-h773az"><div class="MuiBox-root css-1uwjw6e"><div class="MuiBox-root css-d24fs7"><button class="MuiButtonBase-root MuiIconButton-root MuiIconButton-sizeMedium css-1yxmbwk" tabindex="0" type="button"><svg class="MuiSvgIcon-root MuiSvgIcon-fontSizeMedium css-vubbuv" focusable="false" aria-hidden="true" viewbox="0 0 24 24" data-testid="ContentCopyIcon"><path d="M16 1H4c-1.1 0-2 .9-2 2v14h2V3h12V1zm3 4H8c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h11c1.1 0 2-.9 2-2V7c0-1.1-.9-2-2-2zm0 16H8V7h11v14z"/></svg></button></div></div><pre class="prism-code language-php" style="background-color:#2a2734;color:#9a86fd;overflow:auto"><div class="token-line" style="color:#9a86fd"><span class="token plain">for ($i = 0; $i <= 10; $i++) {</span></div><div class="token-line" style="color:#9a86fd"><span class="token plain"> echo $i . '<br>';</span></div><div class="token-line" style="color:#9a86fd"><span class="token plain">}</span></div></pre></div><p class="MuiTypography-root MuiTypography-body1 markdown css-9l3uo3"> </p><p>或者从<code>2</code>到<code>100</code>的2的倍数。</p> <div class="MuiBox-root css-h773az"><div class="MuiBox-root css-1uwjw6e"><div class="MuiBox-root css-d24fs7"><button class="MuiButtonBase-root MuiIconButton-root MuiIconButton-sizeMedium css-1yxmbwk" tabindex="0" type="button"><svg class="MuiSvgIcon-root MuiSvgIcon-fontSizeMedium css-vubbuv" focusable="false" aria-hidden="true" viewbox="0 0 24 24" data-testid="ContentCopyIcon"><path d="M16 1H4c-1.1 0-2 .9-2 2v14h2V3h12V1zm3 4H8c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h11c1.1 0 2-.9 2-2V7c0-1.1-.9-2-2-2zm0 16H8V7h11v14z"/></svg></button></div></div><pre class="prism-code language-php" style="background-color:#2a2734;color:#9a86fd;overflow:auto"><div class="token-line" style="color:#9a86fd"><span class="token plain">for ($i = 2; $i <= 100; $i += 2) {</span></div><div class="token-line" style="color:#9a86fd"><span class="token plain"> echo $i . '<br>';</span></div><div class="token-line" style="color:#9a86fd"><span class="token plain">}</span></div></pre></div><p class="MuiTypography-root MuiTypography-body1 markdown css-9l3uo3"> </p><p>for循环对各种技巧很有用,例如<a href="https://zh.php.brj.cz/abeceda-cisla-intervals">获取字母表、数字数组和间隔</a>。</p> <div id="dang-yi-gedo-while-xun-huan" class="content-anchor"/><h2>当一个do-while循环</h2> <p>只要条件成立,<code>While</code>循环就会运行。while "和 "do-while "的区别在于何时评估条件。</p> <div class="MuiBox-root css-h773az"><div class="MuiBox-root css-1uwjw6e"><div class="MuiBox-root css-d24fs7"><button class="MuiButtonBase-root MuiIconButton-root MuiIconButton-sizeMedium css-1yxmbwk" tabindex="0" type="button"><svg class="MuiSvgIcon-root MuiSvgIcon-fontSizeMedium css-vubbuv" focusable="false" aria-hidden="true" viewbox="0 0 24 24" data-testid="ContentCopyIcon"><path d="M16 1H4c-1.1 0-2 .9-2 2v14h2V3h12V1zm3 4H8c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h11c1.1 0 2-.9 2-2V7c0-1.1-.9-2-2-2zm0 16H8V7h11v14z"/></svg></button></div></div><pre class="prism-code language-php" style="background-color:#2a2734;color:#9a86fd;overflow:auto"><div class="token-line" style="color:#9a86fd"><span class="token plain">while (výraz) {</span></div><div class="token-line" style="color:#9a86fd"><span class="token plain"> // 循环的主体</span></div><div class="token-line" style="color:#9a86fd"><span class="token plain">}</span></div></pre></div><p class="MuiTypography-root MuiTypography-body1 markdown css-9l3uo3"> <div class="MuiBox-root css-h773az"><div class="MuiBox-root css-1uwjw6e"><div class="MuiBox-root css-d24fs7"><button class="MuiButtonBase-root MuiIconButton-root MuiIconButton-sizeMedium css-1yxmbwk" tabindex="0" type="button"><svg class="MuiSvgIcon-root MuiSvgIcon-fontSizeMedium css-vubbuv" focusable="false" aria-hidden="true" viewbox="0 0 24 24" data-testid="ContentCopyIcon"><path d="M16 1H4c-1.1 0-2 .9-2 2v14h2V3h12V1zm3 4H8c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h11c1.1 0 2-.9 2-2V7c0-1.1-.9-2-2-2zm0 16H8V7h11v14z"/></svg></button></div></div><pre class="prism-code language-php" style="background-color:#2a2734;color:#9a86fd;overflow:auto"><div class="token-line" style="color:#9a86fd"><span class="token plain">do {</span></div><div class="token-line" style="color:#9a86fd"><span class="token plain"> // 循环的主体</span></div><div class="token-line" style="color:#9a86fd"><span class="token plain">} while(výraz);</span></div></pre></div><p class="MuiTypography-root MuiTypography-body1 markdown css-9l3uo3"> <code>while</code>首先评估条件,然后运行内循环。</li> <code>do-while</code>首先处理内循环,然后评估条件。</li> <p>当我们不能事先知道循环是否会运行,而想保证它总是至少运行一次时,这一点尤其有利(这时我们使用<code>do-while</code>)。</p> <p>我们有一个数字存储在变量<code>$x = 2</code>中。我们还想在变量<code>$y</code>中生成一个区间<code><0; 10></code>的数字,使其与变量<code>$x</code>中的数字不同。简单地说:在<code>0</code>和<code>10</code>之间生成一个非<code>2</code>的随机数。</p> <p>在这种情况下,使用 "do-while "循环很方便。我们事先不知道这个循环要运行多少次(我们可能不走运,一直碰到同一个数字,在这种情况下,这个循环会运行很长时间),而且我们还想保证在条件评估之前至少运行一次,因为我们必须先生成数字。</p> <div class="MuiBox-root css-h773az"><div class="MuiBox-root css-1uwjw6e"><div class="MuiBox-root css-d24fs7"><button class="MuiButtonBase-root MuiIconButton-root MuiIconButton-sizeMedium css-1yxmbwk" tabindex="0" type="button"><svg class="MuiSvgIcon-root MuiSvgIcon-fontSizeMedium css-vubbuv" focusable="false" aria-hidden="true" viewbox="0 0 24 24" data-testid="ContentCopyIcon"><path d="M16 1H4c-1.1 0-2 .9-2 2v14h2V3h12V1zm3 4H8c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h11c1.1 0 2-.9 2-2V7c0-1.1-.9-2-2-2zm0 16H8V7h11v14z"/></svg></button></div></div><pre class="prism-code language-php" style="background-color:#2a2734;color:#9a86fd;overflow:auto"><div class="token-line" style="color:#9a86fd"><span class="token plain">$x = 2;</span></div><div class="token-line" style="color:#9a86fd"><span class="token plain">$y = $x;</span></div><div class="token-line" style="color:#9a86fd"><span class="token plain" style="display:inline-block"> </span></div><div class="token-line" style="color:#9a86fd"><span class="token plain">do {</span></div><div class="token-line" style="color:#9a86fd"><span class="token plain"> $y = rand(0, 10);</span></div><div class="token-line" style="color:#9a86fd"><span class="token plain">} while($x == $y);</span></div><div class="token-line" style="color:#9a86fd"><span class="token plain" style="display:inline-block"> </span></div><div class="token-line" style="color:#9a86fd"><span class="token plain">echo 'X :' . $x . '<br>';</span></div><div class="token-line" style="color:#9a86fd"><span class="token plain">echo 'Y:' . $y;</span></div></pre></div><p class="MuiTypography-root MuiTypography-body1 markdown css-9l3uo3"> <p><code>foreach</code>是浏览字段和对象的完美选择。我们不仅可以得到具体的值,还可以得到键。</p> <p>如果我们只想获得特定的值。</p> <div class="MuiBox-root css-h773az"><div class="MuiBox-root css-1uwjw6e"><div class="MuiBox-root css-d24fs7"><button class="MuiButtonBase-root MuiIconButton-root MuiIconButton-sizeMedium css-1yxmbwk" tabindex="0" type="button"><svg class="MuiSvgIcon-root MuiSvgIcon-fontSizeMedium css-vubbuv" focusable="false" aria-hidden="true" viewbox="0 0 24 24" data-testid="ContentCopyIcon"><path d="M16 1H4c-1.1 0-2 .9-2 2v14h2V3h12V1zm3 4H8c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h11c1.1 0 2-.9 2-2V7c0-1.1-.9-2-2-2zm0 16H8V7h11v14z"/></svg></button></div></div><pre class="prism-code language-php" style="background-color:#2a2734;color:#9a86fd;overflow:auto"><div class="token-line" style="color:#9a86fd"><span class="token plain">foreach ($data as $value) {</span></div><div class="token-line" style="color:#9a86fd"><span class="token plain"> // 数据处理</span></div><div class="token-line" style="color:#9a86fd"><span class="token plain">}</span></div></pre></div><p class="MuiTypography-root MuiTypography-body1 markdown css-9l3uo3"> </p><p>另外,我们可以拿到钥匙。</p> <div class="MuiBox-root css-h773az"><div class="MuiBox-root css-1uwjw6e"><div class="MuiBox-root css-d24fs7"><button class="MuiButtonBase-root MuiIconButton-root MuiIconButton-sizeMedium css-1yxmbwk" tabindex="0" type="button"><svg class="MuiSvgIcon-root MuiSvgIcon-fontSizeMedium css-vubbuv" focusable="false" aria-hidden="true" viewbox="0 0 24 24" data-testid="ContentCopyIcon"><path d="M16 1H4c-1.1 0-2 .9-2 2v14h2V3h12V1zm3 4H8c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h11c1.1 0 2-.9 2-2V7c0-1.1-.9-2-2-2zm0 16H8V7h11v14z"/></svg></button></div></div><pre class="prism-code language-php" style="background-color:#2a2734;color:#9a86fd;overflow:auto"><div class="token-line" style="color:#9a86fd"><span class="token plain">foreach ($data as $key => $value) {</span></div><div class="token-line" style="color:#9a86fd"><span class="token plain"> // 数据处理</span></div><div class="token-line" style="color:#9a86fd"><span class="token plain">}</span></div></pre></div><p class="MuiTypography-root MuiTypography-body1 markdown css-9l3uo3"> </p><p><code>foreach</code>是浏览真实数据的完美选择--例如,来自数据库的结果,或者有键的字段。</p> <div class="MuiBox-root css-h773az"><div class="MuiBox-root css-1uwjw6e"><div class="MuiBox-root css-d24fs7"><button class="MuiButtonBase-root MuiIconButton-root MuiIconButton-sizeMedium css-1yxmbwk" tabindex="0" type="button"><svg class="MuiSvgIcon-root MuiSvgIcon-fontSizeMedium css-vubbuv" focusable="false" aria-hidden="true" viewbox="0 0 24 24" data-testid="ContentCopyIcon"><path d="M16 1H4c-1.1 0-2 .9-2 2v14h2V3h12V1zm3 4H8c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h11c1.1 0 2-.9 2-2V7c0-1.1-.9-2-2-2zm0 16H8V7h11v14z"/></svg></button></div></div><pre class="prism-code language-php" style="background-color:#2a2734;color:#9a86fd;overflow:auto"><div class="token-line" style="color:#9a86fd"><span class="token plain">$countries = [</span></div><div class="token-line" style="color:#9a86fd"><span class="token plain"> 'en' => '捷克共和国',</span></div><div class="token-line" style="color:#9a86fd"><span class="token plain"> '见' => '斯洛伐克',</span></div><div class="token-line" style="color:#9a86fd"><span class="token plain">];</span></div><div class="token-line" style="color:#9a86fd"><span class="token plain" style="display:inline-block"> </span></div><div class="token-line" style="color:#9a86fd"><span class="token plain">foreach ($countries as $shortcut => $name) {</span></div><div class="token-line" style="color:#9a86fd"><span class="token plain"> echo $shortcut . ':' . $name . '<br>';</span></div><div class="token-line" style="color:#9a86fd"><span class="token plain">}</span></div></pre></div><p class="MuiTypography-root MuiTypography-body1 markdown css-9l3uo3"> <p>递归发生在一个函数或方法调用自己的时候。它的作用是将一个复杂的问题分解成一组小问题。</p> <p>对于初学者来说,理解递归可能是一个挑战,因为它是基于责任传递的理念。</p> <p>该函数实际上说的是 "我不能解决这个问题,但我知道有人可以......",所以它给他打电话,他给别人打电话,......。直到最后一个成员被调用,它就能解决这个问题。</p> <p>当然值得注意的是,任何递归算法都可以被改写成在不需要递归的地方使用循环(反之亦然)。 递归是一个好的仆人,但却是一个坏的主人。它有助于简单和非常有效地解决某些类型的问题,而通过周期循环则对其他事情很有用。</p> <p>一般来说,递归是内存密集型的,因为它一直在创建新的函数和方法的实例和语境。然而,对于遍历树形结构来说,例如,这是解决问题的唯一合理方式。</p> <p>我们需要计算数字<code>5</code>的阶乘,这就是数学中的计算方法。</p> <p><code>5!= 5 * 4 * 3 * 2 * 1</code></p> <p>也就是说,从 "1 "到我们感兴趣的 "阶乘 "的一系列数字有一个连续的乘法。</p> <p>递归地,这可以非常优雅地解决。</p> <div class="MuiBox-root css-h773az"><div class="MuiBox-root css-1uwjw6e"><div class="MuiBox-root css-d24fs7"><button class="MuiButtonBase-root MuiIconButton-root MuiIconButton-sizeMedium css-1yxmbwk" tabindex="0" type="button"><svg class="MuiSvgIcon-root MuiSvgIcon-fontSizeMedium css-vubbuv" focusable="false" aria-hidden="true" viewbox="0 0 24 24" data-testid="ContentCopyIcon"><path d="M16 1H4c-1.1 0-2 .9-2 2v14h2V3h12V1zm3 4H8c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h11c1.1 0 2-.9 2-2V7c0-1.1-.9-2-2-2zm0 16H8V7h11v14z"/></svg></button></div></div><pre class="prism-code language-php" style="background-color:#2a2734;color:#9a86fd;overflow:auto"><div class="token-line" style="color:#9a86fd"><span class="token plain">function factorial(int $n): int</span></div><div class="token-line" style="color:#9a86fd"><span class="token plain">{</span></div><div class="token-line" style="color:#9a86fd"><span class="token plain"> if ($n === 0) {</span></div><div class="token-line" style="color:#9a86fd"><span class="token plain"> return 1;</span></div><div class="token-line" style="color:#9a86fd"><span class="token plain"> }</span></div><div class="token-line" style="color:#9a86fd"><span class="token plain" style="display:inline-block"> </span></div><div class="token-line" style="color:#9a86fd"><span class="token plain"> return $n * factorial($n - 1);</span></div><div class="token-line" style="color:#9a86fd"><span class="token plain">}</span></div></pre></div><p class="MuiTypography-root MuiTypography-body1 markdown css-9l3uo3"> </p><p>或者,使用三元运算符的更短实现。</p> <div class="MuiBox-root css-h773az"><div class="MuiBox-root css-1uwjw6e"><div class="MuiBox-root css-d24fs7"><button class="MuiButtonBase-root MuiIconButton-root MuiIconButton-sizeMedium css-1yxmbwk" tabindex="0" type="button"><svg class="MuiSvgIcon-root MuiSvgIcon-fontSizeMedium css-vubbuv" focusable="false" aria-hidden="true" viewbox="0 0 24 24" data-testid="ContentCopyIcon"><path d="M16 1H4c-1.1 0-2 .9-2 2v14h2V3h12V1zm3 4H8c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h11c1.1 0 2-.9 2-2V7c0-1.1-.9-2-2-2zm0 16H8V7h11v14z"/></svg></button></div></div><pre class="prism-code language-php" style="background-color:#2a2734;color:#9a86fd;overflow:auto"><div class="token-line" style="color:#9a86fd"><span class="token plain">function factorial(int $n): int</span></div><div class="token-line" style="color:#9a86fd"><span class="token plain">{</span></div><div class="token-line" style="color:#9a86fd"><span class="token plain"> return $n === 0 ? 1 : $n * factorial($n - 1);</span></div><div class="token-line" style="color:#9a86fd"><span class="token plain">}</span></div></pre></div><p class="MuiTypography-root MuiTypography-body1 markdown css-9l3uo3"> </p><p>同样可以改写为使用循环。</p> <div class="MuiBox-root css-h773az"><div class="MuiBox-root css-1uwjw6e"><div class="MuiBox-root css-d24fs7"><button class="MuiButtonBase-root MuiIconButton-root MuiIconButton-sizeMedium css-1yxmbwk" tabindex="0" type="button"><svg class="MuiSvgIcon-root MuiSvgIcon-fontSizeMedium css-vubbuv" focusable="false" aria-hidden="true" viewbox="0 0 24 24" data-testid="ContentCopyIcon"><path d="M16 1H4c-1.1 0-2 .9-2 2v14h2V3h12V1zm3 4H8c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h11c1.1 0 2-.9 2-2V7c0-1.1-.9-2-2-2zm0 16H8V7h11v14z"/></svg></button></div></div><pre class="prism-code language-php" style="background-color:#2a2734;color:#9a86fd;overflow:auto"><div class="token-line" style="color:#9a86fd"><span class="token plain">function factorial(int $n): int</span></div><div class="token-line" style="color:#9a86fd"><span class="token plain">{</span></div><div class="token-line" style="color:#9a86fd"><span class="token plain"> $result = 1;</span></div><div class="token-line" style="color:#9a86fd"><span class="token plain" style="display:inline-block"> </span></div><div class="token-line" style="color:#9a86fd"><span class="token plain"> for ($i = $n; $i > 0; $i--) {</span></div><div class="token-line" style="color:#9a86fd"><span class="token plain"> $result *= $i;</span></div><div class="token-line" style="color:#9a86fd"><span class="token plain"> }</span></div><div class="token-line" style="color:#9a86fd"><span class="token plain" style="display:inline-block"> </span></div><div class="token-line" style="color:#9a86fd"><span class="token plain"> return $result;</span></div><div class="token-line" style="color:#9a86fd"><span class="token plain"> }</span></div></pre></div><p class="MuiTypography-root MuiTypography-body1 markdown css-9l3uo3"> </p><p>用周期写的时间稍长,但同样具有明显的内存占用率。我们只使用一个变量<code>$result</code>进行计算,在这里我们不断地改变数值,不必创建新的<code>factorial()</code>的实例。</p> <div id="fei-chang-jin-shen-de-bian-xie-wu-xian-xun-huan" class="content-anchor"/><h2>非常谨慎地编写无限循环</h2> <p>循环很容易变得无限大(我们通过永不满足终止条件来实现这一点)。你应该考虑到这一点,并可能自己用<code>break;</code>来停止循环。</p> <p>例如,掷骰子直到掷出数字<code>6</code>。该实施方案倾向于使用<code>while</code>循环。</p> <div class="MuiBox-root css-h773az"><div class="MuiBox-root css-1uwjw6e"><div class="MuiBox-root css-d24fs7"><button class="MuiButtonBase-root MuiIconButton-root MuiIconButton-sizeMedium css-1yxmbwk" tabindex="0" type="button"><svg class="MuiSvgIcon-root MuiSvgIcon-fontSizeMedium css-vubbuv" focusable="false" aria-hidden="true" viewbox="0 0 24 24" data-testid="ContentCopyIcon"><path d="M16 1H4c-1.1 0-2 .9-2 2v14h2V3h12V1zm3 4H8c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h11c1.1 0 2-.9 2-2V7c0-1.1-.9-2-2-2zm0 16H8V7h11v14z"/></svg></button></div></div><pre class="prism-code language-php" style="background-color:#2a2734;color:#9a86fd;overflow:auto"><div class="token-line" style="color:#9a86fd"><span class="token plain">while (true) {</span></div><div class="token-line" style="color:#9a86fd"><span class="token plain"> $value = rand(1, 6);</span></div><div class="token-line" style="color:#9a86fd"><span class="token plain" style="display:inline-block"> </span></div><div class="token-line" style="color:#9a86fd"><span class="token plain"> if ($value === 6) {</span></div><div class="token-line" style="color:#9a86fd"><span class="token plain"> echo '价值已经下降' . $value;</span></div><div class="token-line" style="color:#9a86fd"><span class="token plain"> break;</span></div><div class="token-line" style="color:#9a86fd"><span class="token plain"> }</span></div><div class="token-line" style="color:#9a86fd"><span class="token plain">}</span></div></pre></div><p class="MuiTypography-root MuiTypography-body1 markdown css-9l3uo3">
推荐文章