1
0
mirror of https://github.com/restoreddev/phpapprentice.git synced 2025-10-23 19:06:05 +02:00
Files
phpapprentice/docs/loops.html
2018-09-02 10:57:36 -05:00

107 lines
5.2 KiB
HTML

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Loops</title>
<meta name="description" content="">
<link rel="stylesheet" href="/css/site.css">
<link rel="icon" href="/favicon-32.png">
<script src="/js/site.js"></script>
</head>
<body>
<div class="menu">
<button class="menu-button" title="Open Menu">
<div class="icon"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20"><path d="M0 3h20v2H0V3zm0 6h20v2H0V9zm0 6h20v2H0v-2z"/></svg></div>
Menu
</button>
</div>
<div class="container center">
<h1>Loops</h1>
<h3 class="subtitle">Increase your repetitions</h3>
<div class="grid-code"><div class="doc"></div><div class="code"><pre><code class="language-php">&lt;?php
</code></pre></div><div class="doc">A loop tells PHP to run a block of code more than once. A classic loop is a while loop. A &quot;while&quot; loop will continue to run the block of code as long as the value in parentheses is true.</div><div class="code"><pre><code class="language-php">$num = 5;
while ($num > 0) {
echo "While loop $num\n";
--$num;
}
</code></pre></div><div class="doc">A &quot;do while&quot; loop is similar to a &quot;while&quot; loop except it always runs at least one iteration. In a classic &quot;while&quot; loop, no iterations may be executed if the value in parentheses is false. In a &quot;do while&quot;, the boolean check is not done until after the execution of an iteration.</div><div class="code"><pre><code class="language-php">$num = 0;
do {
echo "Do while $num\n";
++$num;
} while ($num < 5);
</code></pre></div><div class="doc">&quot;for&quot; loops allow you to create a more concise while loop. Inside the parentheses, the left section creates a variable before the loop starts, the middle section is the check that is done at the beginning of each loop and the third section is executed after each loop.</div><div class="code"><pre><code class="language-php">for ($i = 0; $i < 10; $i++) {
echo "For loop $i\n";
}
</code></pre></div><div class="doc">A &quot;foreach&quot; loop allows you to easily loop over an array. An array is a list of data stored together. The &quot;as&quot; keyword lets you assign a variable to the value in the array for the current iteration of the loop.</div><div class="code"><pre><code class="language-php">$set = [1, 2, 3, 4, 5];
foreach ($set as $num) {
echo "Array value $num\n";
}
</code></pre></div><div class="doc">In loops, you can use the keyword &quot;break&quot; to stop the loop execution no matter how many more iterations should run.</div><div class="code"><pre><code class="language-php">$values = ['one', 'two', 'three'];
foreach ($values as $value) {
if ($value === 'two') {
break;
}
echo "Break $value\n";
}
</code></pre></div><div class="doc">The &quot;continue&quot; keyword stops executing the current loop iteration, but then allows the loop to continue with other iterations.</div><div class="code"><pre><code class="language-php">$values = ['one', 'skip', 'three'];
foreach ($values as $value) {
if ($value === 'skip') {
continue;
}
echo "Continue $value\n";
}
</code></pre></div></div>
<div class="clearfix"></div>
<div class="navigate-links">
<a href="/conditionals.html" title="Previous">
<div class="icon"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20"><path d="M10 20a10 10 0 1 1 0-20 10 10 0 0 1 0 20zm8-10a8 8 0 1 0-16 0 8 8 0 0 0 16 0zM7.46 9.3L11 5.75l1.41 1.41L9.6 10l2.82 2.83L11 14.24 6.76 10l.7-.7z"/></svg></div>
Previous
</a>
<a href="/arrays.html" title="Next">
Next
<div class="icon"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20"><path d="M10 0a10 10 0 1 1 0 20 10 10 0 0 1 0-20zM2 10a8 8 0 1 0 16 0 8 8 0 0 0-16 0zm10.54.7L9 14.25l-1.41-1.41L10.4 10 7.6 7.17 9 5.76 13.24 10l-.7.7z"/></svg></div>
</a>
</div>
</div>
<div class="modal closed">
<div class="modal-content">
<button class="modal-button right" title="Close">
<div class="icon"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20"><path d="M2.93 17.07A10 10 0 1 1 17.07 2.93 10 10 0 0 1 2.93 17.07zm1.41-1.41A8 8 0 1 0 15.66 4.34 8 8 0 0 0 4.34 15.66zm9.9-8.49L11.41 10l2.83 2.83-1.41 1.41L10 11.41l-2.83 2.83-1.41-1.41L8.59 10 5.76 7.17l1.41-1.41L10 8.59l2.83-2.83 1.41 1.41z"/></svg></div>
</button>
<div class="table-of-contents">
<h4>Table of Contents</h4>
<a href="/index.html">Preface</a>
<ol>
<li><a href="/basics.html">Basics</a></li>
<li><a href="/variables.html">Variables</a></li>
<li><a href="/arithmetic.html">Arithmetic</a></li>
<li><a href="/comparisons.html">Comparisons</a></li>
<li><a href="/boolean-logic.html">Boolean Logic</a></li>
<li><a href="/conditionals.html">Conditionals</a></li>
<li><a href="/loops.html">Loops</a></li>
<li><a href="/arrays.html">Arrays</a></li>
<li><a href="/functions.html">Functions</a></li>
<li><a href="/classes.html">Classes</a></li>
</ol>
<a href="/credits.html">Credits</a>
</div>
</div>
</div>
</body>
</html>