{"id":132,"date":"2017-04-21T19:14:28","date_gmt":"2017-04-21T19:14:28","guid":{"rendered":"https:\/\/courses.lumenlearning.com\/suny-the-missing-link-an-introduction-to-web-development-and-programming\/chapter\/chapter-31-structures\/"},"modified":"2017-04-27T18:26:23","modified_gmt":"2017-04-27T18:26:23","slug":"chapter-31-structures","status":"publish","type":"chapter","link":"https:\/\/courses.lumenlearning.com\/suny-the-missing-link-an-introduction-to-web-development-and-programming\/chapter\/chapter-31-structures\/","title":{"raw":"Chapter 31: Structures","rendered":"Chapter 31: Structures"},"content":{"raw":"<div class=\"bc-section section\">\r\n<p class=\"ChapterNumber\">Referred to as selection, control, or loop structures, this set of elements dictate conditions under which certain sections code are executed. They allow our program to be flexible, instead of restricting it to the same actions every iteration. Here, we will consider all of them together, as structures in general.<\/p>\r\n\r\n<h1>If<\/h1>\r\n\u201cIf\u201d is perhaps the simplest test we can apply in logic. In programming, the \u201cthen\u201d side is implied in the code contained in the definition of our statement\u2014If [some condition] is true, then do something. We can use this to put exceptions into our code, or to redirect to a different action. Within the If statement we can perform simple comparisons or have the interpreter perform calculations and get returns from functions as part of its comparison task.\r\n\r\nTo run some examples, we will check if a variable called coffee is equal to hot. If it is, we want to run the drink function.\r\n<pre>if(\"hot\" == $coffee){\r\n\r\n<span class=\"CodeIndent2\">drink($coffee);<\/span>\r\n\r\n}<\/pre>\r\n<div class=\"textbox examples\">\r\n<h3 class=\"red_h\">Additional notes<\/h3>\r\n<p class=\"red_p\">Remember that = is an assignment operation! == or === must be used when testing if both sides of the operand are equivalent.<\/p>\r\n\r\n<\/div>\r\n<h1>Else<\/h1>\r\nNext we will assume our drink() function tells us if we want more or feel full, prompting us to take new actions, like cleaning up after ourselves. This means we need to take one of two actions, which can do by extending our If\/Then by adding Else:\r\n<pre>if(drink($coffee)=='full'){\r\n\r\n<span class=\"CodeIndent1\">cleanUp();<\/span>\r\n\r\n}\r\n\r\nelse{ \/\/We want more!\r\n\r\n<span class=\"CodeIndent2\">drink($coffee);<\/span>\r\n\r\n}<\/pre>\r\n<h1>elseIf<\/h1>\r\nWe can use elseIf when we want to follow up a failed If statement with another check. While the same affect can be created by placing an If inside of our Else, we can eliminate a layer of nesting. We can still follow up an elseIf with its own Else as well:\r\n<pre>if ($a &gt; $b) {\r\n\r\n<span class=\"CodeIndent2\">echo \"a is bigger than b\";<\/span>\r\n\r\n} elseif ($a == $b) {\r\n\r\n<span class=\"CodeIndent2\">echo \"a is equal to b\";<\/span>\r\n\r\n} else {\r\n\r\n<span class=\"CodeIndent2\">echo \"a is smaller than b\";<\/span>\r\n\r\n}<\/pre>\r\n<h1>While<\/h1>\r\nWhile statements will repeatedly execute until the condition we provide is no longer true. The code will start over, in a loop, as long as the requirement is met.\r\n\r\nBe careful with While statements! You can easily create infinite loops using While, in which your code will continue to run until forced to stop by closing the page or resetting your web service. The first method of prevention is to ensure that you have at least one place in your While loop that can affect the value (or values) in your while condition. The second method of prevention is to make sure that at least one result of those changes satisfies your While condition.\r\n\r\nWhile loops are very similar to If in terms of structure. Here we will pretend $ourValue is a 4:\r\n<table class=\"blank\"><colgroup> <col \/> <col \/><\/colgroup>\r\n<tbody>\r\n<tr>\r\n<td>\r\n<pre>$ourValue=4;\r\n\r\nwhile($ourValue!=6){\r\n\r\n<span class=\"CodeIndent2\">echo \"Wait for it...&lt;br\/&gt;\";<\/span>\r\n\r\n<span class=\"CodeIndent2\">$ourValue++;<\/span>\r\n\r\n}\r\n\r\necho \"6!\";<\/pre>\r\n<\/td>\r\n<td class=\"Shaded\">\r\n<pre>Wait for it...\r\n\r\nWait for it...\r\n\r\n6!<\/pre>\r\n<\/td>\r\n<\/tr>\r\n<\/tbody>\r\n<\/table>\r\nLet us try one that counts for us:\r\n<table class=\"blank\"><colgroup> <col \/> <col \/><\/colgroup>\r\n<tbody>\r\n<tr>\r\n<td>\r\n<pre>$ourValue=4;\r\n\r\nwhile($ourValue!=10){\r\n\r\n<span class=\"CodeIndent2\">echo \"<strong class=\"bold_code\">$ourValue,\" ;<\/strong><\/span>\r\n\r\n<span class=\"CodeIndent2\">$ourValue++;<\/span>\r\n\r\n}<\/pre>\r\n<\/td>\r\n<td class=\"Shaded\">\r\n<pre>4, 5, 6, 7, 8, 9,<\/pre>\r\n<\/td>\r\n<\/tr>\r\n<\/tbody>\r\n<\/table>\r\n<p class=\"Centered\">To see a broken While statement in action, change $ourValue to 11 and refresh your page!<\/p>\r\n\r\n<h1>Do While<\/h1>\r\nDo While loops are very similar to While loops with the difference that every Do While loop will run at least one time. This is because a Do While loop evaluates the equation after each loop, not before. Since it does not check if conditions are met until the end, the first loop will never be checked. For example, compare the following loops and their output:\r\n<table class=\"blank\"><colgroup> <col \/> <col \/><\/colgroup>\r\n<tbody>\r\n<tr>\r\n<td>\r\n<pre>do {\r\n\r\n<span class=\"CodeIndent1\">echo $i;<\/span>\r\n\r\n} while ($i &gt; 0);\r\n\r\necho \u201c&lt;br\/&gt; done\u201d;<\/pre>\r\n<\/td>\r\n<td class=\"Shaded\">\r\n<pre>0\r\n\r\ndone<\/pre>\r\n<\/td>\r\n<\/tr>\r\n<tr>\r\n<td>\r\n<pre>while ($i&gt;0){\r\n\r\n<span class=\"CodeIndent1\">echo $i;<\/span>\r\n\r\n}\r\n\r\necho \u201c&lt;br\/&gt; done\u201d;<\/pre>\r\n<\/td>\r\n<td class=\"Shaded\">\r\n<pre>done<\/pre>\r\n<\/td>\r\n<\/tr>\r\n<\/tbody>\r\n<\/table>\r\n<div class=\"textbox exercises\">\r\n<h3 class=\"learn-h\">Learn more<\/h3>\r\n<p class=\"learn-m\">Keywords, search terms: Control structures, logic<\/p>\r\n<p class=\"learn-m\">Alternative syntax: <a href=\"http:\/\/www.brian2000.com\/php\/understanding-alternative-syntax-for-control-structures-in-php\/\"><span class=\"Hyperlink\">http:\/\/www.brian2000.com\/php\/understanding-alternative-<\/span><span class=\"Hyperlink\">syntax-for-control-structures-in-php<\/span><\/a><span class=\"Hyperlink\">\/<\/span><\/p>\r\n<p class=\"learn-m\">More examples: <a href=\"http:\/\/www.informit.com\/articles\/article.aspx?p=30092&amp;seqNum=2\"><span class=\"Hyperlink\">http:\/\/www.informit.com\/articles\/article.aspx?p=30092&amp;seqNum=<\/span><\/a><span class=\"Hyperlink\">2<\/span><\/p>\r\n\r\n<\/div>\r\n<h1>For<\/h1>\r\nFor loops are very similar to While loops, but they execute code only while the condition is within the given range. When using a For loop, we need to provide it with the value we want to monitor, the condition we want to stop on, and how we want to change it, right in the declaration. As a result, the body of the loop is strictly what we want to happen while the For\u2019s conditions are true. Let us say we want to watch $x (we will pretend it is 5 right now). We want to keep running until it is greater than 10, and we want to add 2 each time we finish the loop:\r\n<table id=\"table-15\" class=\"blank\"><colgroup> <col \/> <col \/><\/colgroup>\r\n<tbody>\r\n<tr>\r\n<td>\r\n<pre>$x=5;\r\n\r\nfor($x;$x&lt;10;$x+=2){\r\n\r\n<span class=\"CodeIndent2\">echo \"$x is less than 10 &lt;br\/&gt;\";<\/span>\r\n\r\n}\r\n\r\necho \"$x is greater than 10!\";<\/pre>\r\n<\/td>\r\n<td class=\"Shaded\">\r\n<pre>5 is less than 10\r\n\r\n7 is less than 10\r\n\r\n9 is less than 10\r\n\r\n11 is greater than 10!<\/pre>\r\n<\/td>\r\n<\/tr>\r\n<\/tbody>\r\n<\/table>\r\nUnlike the While loop, you will notice we did not have to change the value of $x inside our loop, it was done automatically each time the end of the loop was reached. If the termination condition (in our example x would be greater or equal to 10) is not met, the loop starts again. Let us look at another example. This time, if a secondary condition that we test for occurs, we will force the For to stop by changing $x ourselves to something that satisfies the termination condition:\r\n<table class=\"blank\"><colgroup> <col \/> <col \/><\/colgroup>\r\n<tbody>\r\n<tr>\r\n<td>\r\n<pre>$x=5;\r\n\r\nfor($x;$x&lt;10;$x+=2){\r\n\r\n<span class=\"CodeIndent2\">echo \"$x is less than 10 &lt;br\/&gt;\";<\/span>\r\n\r\n<span class=\"CodeIndent2\">if($x==7)$x=11;<\/span>\r\n\r\n}\r\n\r\necho \"$x is greater than 10!\";<\/pre>\r\n<\/td>\r\n<td class=\"Shaded\">\r\n<pre>5 is less than 10\r\n\r\n7 is less than 10\r\n\r\n11 is greater than 10!<\/pre>\r\n<\/td>\r\n<\/tr>\r\n<\/tbody>\r\n<\/table>\r\n<h1>Foreach<\/h1>\r\nAlthough we took a brief look at Foreach when we reviewed <a href=\"https:\/\/courses.lumenlearning.com\/suny-the-missing-link-an-introduction-to-web-development-and-programming\/chapter\/chapter-6-development\/#pseudo-code\"><span class=\"Hyperlink\">pseudo-code<\/span><\/a>, we will take a look at an actual implementation now. This loop format is used to iterate over arrays, allowing us an opportunity to manipulate each element inside. Since it is designed specifically for interacting with arrays, these are the only objects you can pass to it. The function takes the array we want to look at as well as what we want to call each element as we look at it. Alternatively, we can ask that the function take each element as a key and value pair, which can be very useful when position value is needed or associative arrays are in play.\r\n\r\nWhen we use a Foreach, the engine makes a copy of the array, leaving the original intact, eliminating the need to track and reset the original array\u2019s pointer or reset the array when we are done. This also means that while we are interacting with the elements inside our array, we need to keep in mind that we are dealing with a copy. No changes that are applied to our copy will persist unless we call the function while applying an ampersand (&amp;) before the variable name, which instructs the function to apply changes directly to our original array.\r\n\r\nIn order to apply changes to our array, the original that is passed must be a variable already stored in memory, and not an array declared in the function call. Let us look at some examples to clear some of this up by mimicking print_r().\r\n<table id=\"table-17\" class=\"blank\"><colgroup> <col \/> <col \/><\/colgroup>\r\n<tbody>\r\n<tr>\r\n<td class=\"Table-Large\">\r\n<pre>$array = array(1, 2, 3, 4, 5);\r\n\r\nforeach($array as $number){\r\n\r\n<span class=\"CodeIndent2\">echo \"Our value is $number &lt;br \/&gt;\";<\/span>\r\n\r\n}<\/pre>\r\n<\/td>\r\n<td class=\"Shaded\">\r\n<pre>Our value is 1\r\n\r\nOur value is 2\r\n\r\nOur value is 3\r\n\r\nOur value is 4\r\n\r\nOur value is 5<\/pre>\r\n<\/td>\r\n<\/tr>\r\n<\/tbody>\r\n<\/table>\r\nTo play with an associative array and see the key and the value, we will adjust both our starting array and what we pass in the function call:\r\n<table class=\"blank\"><colgroup> <col \/> <col \/><\/colgroup>\r\n<tbody>\r\n<tr>\r\n<td class=\"blank\">\r\n<pre>$array = array(\"Mike\"=&gt;42, \"Frank\"=&gt;38, \"Anne\"=&gt;28);\r\n\r\nforeach($array as $key=&gt;$value){\r\n\r\n<span class=\"CodeIndent2\">echo \"$key is $value years old.&lt;br \/&gt;\";<\/span>\r\n\r\n}<\/pre>\r\n<\/td>\r\n<td class=\"Shaded\">\r\n<pre>Mike is 42 years old.\r\n\r\nFrank is 38 years old.\r\n\r\nAnne is 28 years old.<\/pre>\r\n<\/td>\r\n<\/tr>\r\n<\/tbody>\r\n<\/table>\r\nFinally, we will take a look at applying our changes back to the array, multiplying our original array\u2019s values by 2:\r\n<table id=\"table-19\" class=\"blank\"><colgroup> <col \/> <col \/><\/colgroup>\r\n<tbody>\r\n<tr>\r\n<td class=\"blank\">\r\n<pre>$array = array(1, 2, 3, 4, 5);\r\n\r\nforeach($array as &amp;$number){\r\n\r\n<span class=\"CodeIndent2\">$number = $number * 2;<\/span>\r\n\r\n}\r\n\r\nprint_r($array);<\/pre>\r\n<\/td>\r\n<td class=\"Shaded\">\r\n<pre>Array(\r\n\r\n<span class=\"CodeIndent1\">0 =&gt; 2<\/span>\r\n\r\n<span class=\"CodeIndent1\">1 =&gt; 4<\/span>\r\n\r\n<span class=\"CodeIndent1\">2 =&gt; 6<\/span>\r\n\r\n<span class=\"CodeIndent1\">3 =&gt; 8<\/span>\r\n\r\n<span class=\"CodeIndent1\">4 =&gt; 10<\/span>\r\n\r\n)<\/pre>\r\n<\/td>\r\n<\/tr>\r\n<\/tbody>\r\n<\/table>\r\n<h1>Switch<\/h1>\r\nA switch statement lets us run several tests to determine the proper course of action, or to apply one or more changes to our variable or elsewhere in our code, when the condition of our case is met. Some indicators that a switch is appropriate are when you find yourself running several If Then statements on the same variable, or have nested multiple logic statements attempting to control the action of your script.\r\n\r\nTo create an example, let us write a switch that gives information about a number passed to it. First, we will design it to determine the smallest positive value of 2 through 9 that $value is a multiple of, and then we will tweak it a bit so it can tell us all the values of 2 through 9 $value is a multiple of. If we just want the smallest value, we only need to test 2, 3, 5, and 7 since 4, 6, 8 and 9 are multiples of these numbers anyway, so they could not be the smallest. Now, if the number we check is divisible by one of these numbers, it would not have a remainder. To check specifically for a remainder, we can use modular division, which in PHP is represented by a %, and returns a 0 or 1. If we get a zero, there is no remainder. So let us create a switch with empty test cases for 2, 3, 5 and 7:\r\n<pre>switch($value){\r\n\r\n<span class=\"CodeIndent2\">case ($value % 2 == 0 ):<\/span>\r\n\r\n<span class=\"CodeIndent2\">case ($value % 3 == 0 ):<\/span>\r\n\r\n<span class=\"CodeIndent2\">case ($value % 5 == 0 ):<\/span>\r\n\r\n<span class=\"CodeIndent2\">case ($value % 7 == 0 ):<\/span>\r\n\r\n<span class=\"CodeIndent2\">default:<\/span>\r\n\r\n}<\/pre>\r\nEach case can be followed by a set of parenthesis denoting our logical test or a value, which is followed by a colon that denotes the start of what we want to happen when the case is true. If we wanted a case where the value IS 2, we would use:\r\n<pre>Case 2:<\/pre>\r\nOr, if we wanted to test if the value is the WORD two, we would use:\r\n<pre>Case \"two\":<\/pre>\r\nBy default, each case will be tested until the switch is complete, or we tell it we want it to stop. This ability will be useful in a moment, but for now, we want the switch to stop as soon as we find a value, since we are testing in smallest to largest order, and only need one test to be true. To do this, we put a \u201cbreak;\u201c wherever we want the execution of our switch to stop. Typically this is the last line before the following case, but if the case we are in has additional logic tests we might have more than one \u201cbreak;\u201d depending on the extra conditions we are testing.\r\n\r\nYou will also notice the \u201cdefault:\u201d that snuck in at the bottom. The default case must be last, but is optional, and gives us a \u201ccatch all\u201d action in the event that none of our cases were met. We do not need a \u201cbreak;\u201d after our default, since it will always be the last case in our switch.\r\n\r\nTo complete our example, we will want to let the user know what the smallest value we found was, so we need to fill out our code:\r\n<pre>switch($value){\r\n\r\ncase ($value % 2 == 0 ):\r\n\r\n<span class=\"CodeIndent1\">echo \"$value is divisible by 2 &lt;br\/&gt;\";<\/span>\r\n\r\n<span class=\"CodeIndent1\">break;<\/span>\r\n\r\ncase ($value % 3 == 0 ):\r\n\r\n<span class=\"CodeIndent1\">echo \"$value is divisible by 3 &lt;br\/&gt;\";<\/span>\r\n\r\n<span class=\"CodeIndent1\">break;<\/span>\r\n\r\ncase ($value % 5 == 0 ):\r\n\r\n<span class=\"CodeIndent1\">echo \"$value is divisible by 5 &lt;br\/&gt;\";<\/span>\r\n\r\n<span class=\"CodeIndent1\">break;<\/span>\r\n\r\ncase ($value % 7 == 0 ):\r\n\r\n<span class=\"CodeIndent1\">echo \"$value is divisible by 7 &lt;br\/&gt;\";<\/span>\r\n\r\n<span class=\"CodeIndent1\">break;<\/span>\r\n\r\ndefault:\r\n\r\n<span class=\"CodeIndent1\">echo \"$value is not divisible by 2 through 9.\";<\/span>\r\n\r\n}<\/pre>\r\n<div class=\"textbox examples\">\r\n<h3 class=\"red_h\">Additional notes<\/h3>\r\n<p class=\"red_p\">This does not mean our switch will find all prime numbers! Prime numbers have to be tested against the range 2 to n<span class=\"Superscript\">1\/2<\/span> to ensure there are no dividends.<\/p>\r\n\r\n<\/div>\r\nWith this example, if $value was 4, we would get \u201c4 is divisible by 2.\u201d If $value was 12, we would get \u201c12 is divisible by 2\u201d but would not get a response for 3, 4, or 6 since we included breaks after each test, and it stopped after 2. If $value was 11, we would get all the way to \u201c11 is not divisible by 2 through 9.\u201d In this scenario, it is because 11 is a prime number, which by definition is only divisible by itself and 1.\r\n\r\nNow let us tweak our switch statement so it tells us all of the values between 2 and 9 that can divide our number without a remainder. First, we will have to start testing the values we could skip earlier. For example, 8 is not divisible by 6 even though both are divisible by 2. Second, we no longer want to stop after one expression is true. To do this, we will get rid of all of our breaks except for the one in the case preceding the default, ensuring that if any of the cases were true, we will not still see our default statement. That gives us:\r\n<pre>switch($value){\r\n\r\ncase ($value % 2 == 0 ):\r\n\r\n<span class=\"CodeIndent1\">echo \"$value is divisible by 2 &lt;br\/&gt;\";<\/span>\r\n\r\ncase ($value % 3 == 0 ):\r\n\r\n<span class=\"CodeIndent1\">echo \"$value is divisible by 3 &lt;br\/&gt;\";<\/span>\r\n\r\ncase ($value % 4 == 0 ):\r\n\r\n<span class=\"CodeIndent1\">echo \"$value is divisible by 4 &lt;br\/&gt;\";<\/span>\r\n\r\ncase ($value % 5 == 0 ):\r\n\r\n<span class=\"CodeIndent1\">echo \"$value is divisible by 5 &lt;br\/&gt;\";<\/span>\r\n\r\ncase ($value % 6 == 0 ):\r\n\r\n<span class=\"CodeIndent1\">echo \"$value is divisible by 6 &lt;br\/&gt;\";<\/span>\r\n\r\ncase ($value % 7 == 0 ):\r\n\r\n<span class=\"CodeIndent1\">echo \"$value is divisible by 7 &lt;br\/&gt;\";<\/span>\r\n\r\ncase ($value % 8 == 0 ):\r\n\r\n<span class=\"CodeIndent1\">echo \"$value is divisible by 8 &lt;br\/&gt;\";<\/span>\r\n\r\ncase ($value % 9 == 0 ):\r\n\r\n<span class=\"CodeIndent1\">echo \"$value is divisible by 9 &lt;br\/&gt;\";<\/span>\r\n\r\n<span class=\"CodeIndent1\">break;<\/span>\r\n\r\ndefault:\r\n\r\n<span class=\"CodeIndent1\">echo \"$value is not divisible by 2 through 9.\";<\/span>\r\n\r\n}<\/pre>\r\nTo repeat our examples using 4, 12, and 11, respectfully we would see the following responses:\r\n<pre>4 is divisible by 2\r\n\r\n4 is divisible by 4\r\n\r\n12 is divisible by 2\r\n\r\n12 is divisible by 3\r\n\r\n12 is divisible by 4\r\n\r\n12 is divisible by 6\r\n\r\n11 is not divisible by 2 through 9<\/pre>\r\n<\/div>","rendered":"<div class=\"bc-section section\">\n<p class=\"ChapterNumber\">Referred to as selection, control, or loop structures, this set of elements dictate conditions under which certain sections code are executed. They allow our program to be flexible, instead of restricting it to the same actions every iteration. Here, we will consider all of them together, as structures in general.<\/p>\n<h1>If<\/h1>\n<p>\u201cIf\u201d is perhaps the simplest test we can apply in logic. In programming, the \u201cthen\u201d side is implied in the code contained in the definition of our statement\u2014If [some condition] is true, then do something. We can use this to put exceptions into our code, or to redirect to a different action. Within the If statement we can perform simple comparisons or have the interpreter perform calculations and get returns from functions as part of its comparison task.<\/p>\n<p>To run some examples, we will check if a variable called coffee is equal to hot. If it is, we want to run the drink function.<\/p>\n<pre>if(\"hot\" == $coffee){\r\n\r\n<span class=\"CodeIndent2\">drink($coffee);<\/span>\r\n\r\n}<\/pre>\n<div class=\"textbox examples\">\n<h3 class=\"red_h\">Additional notes<\/h3>\n<p class=\"red_p\">Remember that = is an assignment operation! == or === must be used when testing if both sides of the operand are equivalent.<\/p>\n<\/div>\n<h1>Else<\/h1>\n<p>Next we will assume our drink() function tells us if we want more or feel full, prompting us to take new actions, like cleaning up after ourselves. This means we need to take one of two actions, which can do by extending our If\/Then by adding Else:<\/p>\n<pre>if(drink($coffee)=='full'){\r\n\r\n<span class=\"CodeIndent1\">cleanUp();<\/span>\r\n\r\n}\r\n\r\nelse{ \/\/We want more!\r\n\r\n<span class=\"CodeIndent2\">drink($coffee);<\/span>\r\n\r\n}<\/pre>\n<h1>elseIf<\/h1>\n<p>We can use elseIf when we want to follow up a failed If statement with another check. While the same affect can be created by placing an If inside of our Else, we can eliminate a layer of nesting. We can still follow up an elseIf with its own Else as well:<\/p>\n<pre>if ($a &gt; $b) {\r\n\r\n<span class=\"CodeIndent2\">echo \"a is bigger than b\";<\/span>\r\n\r\n} elseif ($a == $b) {\r\n\r\n<span class=\"CodeIndent2\">echo \"a is equal to b\";<\/span>\r\n\r\n} else {\r\n\r\n<span class=\"CodeIndent2\">echo \"a is smaller than b\";<\/span>\r\n\r\n}<\/pre>\n<h1>While<\/h1>\n<p>While statements will repeatedly execute until the condition we provide is no longer true. The code will start over, in a loop, as long as the requirement is met.<\/p>\n<p>Be careful with While statements! You can easily create infinite loops using While, in which your code will continue to run until forced to stop by closing the page or resetting your web service. The first method of prevention is to ensure that you have at least one place in your While loop that can affect the value (or values) in your while condition. The second method of prevention is to make sure that at least one result of those changes satisfies your While condition.<\/p>\n<p>While loops are very similar to If in terms of structure. Here we will pretend $ourValue is a 4:<\/p>\n<table class=\"blank\">\n<colgroup>\n<col \/>\n<col \/><\/colgroup>\n<tbody>\n<tr>\n<td>\n<pre>$ourValue=4;\r\n\r\nwhile($ourValue!=6){\r\n\r\n<span class=\"CodeIndent2\">echo \"Wait for it...&lt;br\/&gt;\";<\/span>\r\n\r\n<span class=\"CodeIndent2\">$ourValue++;<\/span>\r\n\r\n}\r\n\r\necho \"6!\";<\/pre>\n<\/td>\n<td class=\"Shaded\">\n<pre>Wait for it...\r\n\r\nWait for it...\r\n\r\n6!<\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>Let us try one that counts for us:<\/p>\n<table class=\"blank\">\n<colgroup>\n<col \/>\n<col \/><\/colgroup>\n<tbody>\n<tr>\n<td>\n<pre>$ourValue=4;\r\n\r\nwhile($ourValue!=10){\r\n\r\n<span class=\"CodeIndent2\">echo \"<strong class=\"bold_code\">$ourValue,\" ;<\/strong><\/span>\r\n\r\n<span class=\"CodeIndent2\">$ourValue++;<\/span>\r\n\r\n}<\/pre>\n<\/td>\n<td class=\"Shaded\">\n<pre>4, 5, 6, 7, 8, 9,<\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p class=\"Centered\">To see a broken While statement in action, change $ourValue to 11 and refresh your page!<\/p>\n<h1>Do While<\/h1>\n<p>Do While loops are very similar to While loops with the difference that every Do While loop will run at least one time. This is because a Do While loop evaluates the equation after each loop, not before. Since it does not check if conditions are met until the end, the first loop will never be checked. For example, compare the following loops and their output:<\/p>\n<table class=\"blank\">\n<colgroup>\n<col \/>\n<col \/><\/colgroup>\n<tbody>\n<tr>\n<td>\n<pre>do {\r\n\r\n<span class=\"CodeIndent1\">echo $i;<\/span>\r\n\r\n} while ($i &gt; 0);\r\n\r\necho \u201c&lt;br\/&gt; done\u201d;<\/pre>\n<\/td>\n<td class=\"Shaded\">\n<pre>0\r\n\r\ndone<\/pre>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<pre>while ($i&gt;0){\r\n\r\n<span class=\"CodeIndent1\">echo $i;<\/span>\r\n\r\n}\r\n\r\necho \u201c&lt;br\/&gt; done\u201d;<\/pre>\n<\/td>\n<td class=\"Shaded\">\n<pre>done<\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<div class=\"textbox exercises\">\n<h3 class=\"learn-h\">Learn more<\/h3>\n<p class=\"learn-m\">Keywords, search terms: Control structures, logic<\/p>\n<p class=\"learn-m\">Alternative syntax: <a href=\"http:\/\/www.brian2000.com\/php\/understanding-alternative-syntax-for-control-structures-in-php\/\"><span class=\"Hyperlink\">http:\/\/www.brian2000.com\/php\/understanding-alternative-<\/span><span class=\"Hyperlink\">syntax-for-control-structures-in-php<\/span><\/a><span class=\"Hyperlink\">\/<\/span><\/p>\n<p class=\"learn-m\">More examples: <a href=\"http:\/\/www.informit.com\/articles\/article.aspx?p=30092&amp;seqNum=2\"><span class=\"Hyperlink\">http:\/\/www.informit.com\/articles\/article.aspx?p=30092&amp;seqNum=<\/span><\/a><span class=\"Hyperlink\">2<\/span><\/p>\n<\/div>\n<h1>For<\/h1>\n<p>For loops are very similar to While loops, but they execute code only while the condition is within the given range. When using a For loop, we need to provide it with the value we want to monitor, the condition we want to stop on, and how we want to change it, right in the declaration. As a result, the body of the loop is strictly what we want to happen while the For\u2019s conditions are true. Let us say we want to watch $x (we will pretend it is 5 right now). We want to keep running until it is greater than 10, and we want to add 2 each time we finish the loop:<\/p>\n<table id=\"table-15\" class=\"blank\">\n<colgroup>\n<col \/>\n<col \/><\/colgroup>\n<tbody>\n<tr>\n<td>\n<pre>$x=5;\r\n\r\nfor($x;$x&lt;10;$x+=2){\r\n\r\n<span class=\"CodeIndent2\">echo \"$x is less than 10 &lt;br\/&gt;\";<\/span>\r\n\r\n}\r\n\r\necho \"$x is greater than 10!\";<\/pre>\n<\/td>\n<td class=\"Shaded\">\n<pre>5 is less than 10\r\n\r\n7 is less than 10\r\n\r\n9 is less than 10\r\n\r\n11 is greater than 10!<\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>Unlike the While loop, you will notice we did not have to change the value of $x inside our loop, it was done automatically each time the end of the loop was reached. If the termination condition (in our example x would be greater or equal to 10) is not met, the loop starts again. Let us look at another example. This time, if a secondary condition that we test for occurs, we will force the For to stop by changing $x ourselves to something that satisfies the termination condition:<\/p>\n<table class=\"blank\">\n<colgroup>\n<col \/>\n<col \/><\/colgroup>\n<tbody>\n<tr>\n<td>\n<pre>$x=5;\r\n\r\nfor($x;$x&lt;10;$x+=2){\r\n\r\n<span class=\"CodeIndent2\">echo \"$x is less than 10 &lt;br\/&gt;\";<\/span>\r\n\r\n<span class=\"CodeIndent2\">if($x==7)$x=11;<\/span>\r\n\r\n}\r\n\r\necho \"$x is greater than 10!\";<\/pre>\n<\/td>\n<td class=\"Shaded\">\n<pre>5 is less than 10\r\n\r\n7 is less than 10\r\n\r\n11 is greater than 10!<\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h1>Foreach<\/h1>\n<p>Although we took a brief look at Foreach when we reviewed <a href=\"https:\/\/courses.lumenlearning.com\/suny-the-missing-link-an-introduction-to-web-development-and-programming\/chapter\/chapter-6-development\/#pseudo-code\"><span class=\"Hyperlink\">pseudo-code<\/span><\/a>, we will take a look at an actual implementation now. This loop format is used to iterate over arrays, allowing us an opportunity to manipulate each element inside. Since it is designed specifically for interacting with arrays, these are the only objects you can pass to it. The function takes the array we want to look at as well as what we want to call each element as we look at it. Alternatively, we can ask that the function take each element as a key and value pair, which can be very useful when position value is needed or associative arrays are in play.<\/p>\n<p>When we use a Foreach, the engine makes a copy of the array, leaving the original intact, eliminating the need to track and reset the original array\u2019s pointer or reset the array when we are done. This also means that while we are interacting with the elements inside our array, we need to keep in mind that we are dealing with a copy. No changes that are applied to our copy will persist unless we call the function while applying an ampersand (&amp;) before the variable name, which instructs the function to apply changes directly to our original array.<\/p>\n<p>In order to apply changes to our array, the original that is passed must be a variable already stored in memory, and not an array declared in the function call. Let us look at some examples to clear some of this up by mimicking print_r().<\/p>\n<table id=\"table-17\" class=\"blank\">\n<colgroup>\n<col \/>\n<col \/><\/colgroup>\n<tbody>\n<tr>\n<td class=\"Table-Large\">\n<pre>$array = array(1, 2, 3, 4, 5);\r\n\r\nforeach($array as $number){\r\n\r\n<span class=\"CodeIndent2\">echo \"Our value is $number &lt;br \/&gt;\";<\/span>\r\n\r\n}<\/pre>\n<\/td>\n<td class=\"Shaded\">\n<pre>Our value is 1\r\n\r\nOur value is 2\r\n\r\nOur value is 3\r\n\r\nOur value is 4\r\n\r\nOur value is 5<\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>To play with an associative array and see the key and the value, we will adjust both our starting array and what we pass in the function call:<\/p>\n<table class=\"blank\">\n<colgroup>\n<col \/>\n<col \/><\/colgroup>\n<tbody>\n<tr>\n<td class=\"blank\">\n<pre>$array = array(\"Mike\"=&gt;42, \"Frank\"=&gt;38, \"Anne\"=&gt;28);\r\n\r\nforeach($array as $key=&gt;$value){\r\n\r\n<span class=\"CodeIndent2\">echo \"$key is $value years old.&lt;br \/&gt;\";<\/span>\r\n\r\n}<\/pre>\n<\/td>\n<td class=\"Shaded\">\n<pre>Mike is 42 years old.\r\n\r\nFrank is 38 years old.\r\n\r\nAnne is 28 years old.<\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>Finally, we will take a look at applying our changes back to the array, multiplying our original array\u2019s values by 2:<\/p>\n<table id=\"table-19\" class=\"blank\">\n<colgroup>\n<col \/>\n<col \/><\/colgroup>\n<tbody>\n<tr>\n<td class=\"blank\">\n<pre>$array = array(1, 2, 3, 4, 5);\r\n\r\nforeach($array as &amp;$number){\r\n\r\n<span class=\"CodeIndent2\">$number = $number * 2;<\/span>\r\n\r\n}\r\n\r\nprint_r($array);<\/pre>\n<\/td>\n<td class=\"Shaded\">\n<pre>Array(\r\n\r\n<span class=\"CodeIndent1\">0 =&gt; 2<\/span>\r\n\r\n<span class=\"CodeIndent1\">1 =&gt; 4<\/span>\r\n\r\n<span class=\"CodeIndent1\">2 =&gt; 6<\/span>\r\n\r\n<span class=\"CodeIndent1\">3 =&gt; 8<\/span>\r\n\r\n<span class=\"CodeIndent1\">4 =&gt; 10<\/span>\r\n\r\n)<\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h1>Switch<\/h1>\n<p>A switch statement lets us run several tests to determine the proper course of action, or to apply one or more changes to our variable or elsewhere in our code, when the condition of our case is met. Some indicators that a switch is appropriate are when you find yourself running several If Then statements on the same variable, or have nested multiple logic statements attempting to control the action of your script.<\/p>\n<p>To create an example, let us write a switch that gives information about a number passed to it. First, we will design it to determine the smallest positive value of 2 through 9 that $value is a multiple of, and then we will tweak it a bit so it can tell us all the values of 2 through 9 $value is a multiple of. If we just want the smallest value, we only need to test 2, 3, 5, and 7 since 4, 6, 8 and 9 are multiples of these numbers anyway, so they could not be the smallest. Now, if the number we check is divisible by one of these numbers, it would not have a remainder. To check specifically for a remainder, we can use modular division, which in PHP is represented by a %, and returns a 0 or 1. If we get a zero, there is no remainder. So let us create a switch with empty test cases for 2, 3, 5 and 7:<\/p>\n<pre>switch($value){\r\n\r\n<span class=\"CodeIndent2\">case ($value % 2 == 0 ):<\/span>\r\n\r\n<span class=\"CodeIndent2\">case ($value % 3 == 0 ):<\/span>\r\n\r\n<span class=\"CodeIndent2\">case ($value % 5 == 0 ):<\/span>\r\n\r\n<span class=\"CodeIndent2\">case ($value % 7 == 0 ):<\/span>\r\n\r\n<span class=\"CodeIndent2\">default:<\/span>\r\n\r\n}<\/pre>\n<p>Each case can be followed by a set of parenthesis denoting our logical test or a value, which is followed by a colon that denotes the start of what we want to happen when the case is true. If we wanted a case where the value IS 2, we would use:<\/p>\n<pre>Case 2:<\/pre>\n<p>Or, if we wanted to test if the value is the WORD two, we would use:<\/p>\n<pre>Case \"two\":<\/pre>\n<p>By default, each case will be tested until the switch is complete, or we tell it we want it to stop. This ability will be useful in a moment, but for now, we want the switch to stop as soon as we find a value, since we are testing in smallest to largest order, and only need one test to be true. To do this, we put a \u201cbreak;\u201c wherever we want the execution of our switch to stop. Typically this is the last line before the following case, but if the case we are in has additional logic tests we might have more than one \u201cbreak;\u201d depending on the extra conditions we are testing.<\/p>\n<p>You will also notice the \u201cdefault:\u201d that snuck in at the bottom. The default case must be last, but is optional, and gives us a \u201ccatch all\u201d action in the event that none of our cases were met. We do not need a \u201cbreak;\u201d after our default, since it will always be the last case in our switch.<\/p>\n<p>To complete our example, we will want to let the user know what the smallest value we found was, so we need to fill out our code:<\/p>\n<pre>switch($value){\r\n\r\ncase ($value % 2 == 0 ):\r\n\r\n<span class=\"CodeIndent1\">echo \"$value is divisible by 2 &lt;br\/&gt;\";<\/span>\r\n\r\n<span class=\"CodeIndent1\">break;<\/span>\r\n\r\ncase ($value % 3 == 0 ):\r\n\r\n<span class=\"CodeIndent1\">echo \"$value is divisible by 3 &lt;br\/&gt;\";<\/span>\r\n\r\n<span class=\"CodeIndent1\">break;<\/span>\r\n\r\ncase ($value % 5 == 0 ):\r\n\r\n<span class=\"CodeIndent1\">echo \"$value is divisible by 5 &lt;br\/&gt;\";<\/span>\r\n\r\n<span class=\"CodeIndent1\">break;<\/span>\r\n\r\ncase ($value % 7 == 0 ):\r\n\r\n<span class=\"CodeIndent1\">echo \"$value is divisible by 7 &lt;br\/&gt;\";<\/span>\r\n\r\n<span class=\"CodeIndent1\">break;<\/span>\r\n\r\ndefault:\r\n\r\n<span class=\"CodeIndent1\">echo \"$value is not divisible by 2 through 9.\";<\/span>\r\n\r\n}<\/pre>\n<div class=\"textbox examples\">\n<h3 class=\"red_h\">Additional notes<\/h3>\n<p class=\"red_p\">This does not mean our switch will find all prime numbers! Prime numbers have to be tested against the range 2 to n<span class=\"Superscript\">1\/2<\/span> to ensure there are no dividends.<\/p>\n<\/div>\n<p>With this example, if $value was 4, we would get \u201c4 is divisible by 2.\u201d If $value was 12, we would get \u201c12 is divisible by 2\u201d but would not get a response for 3, 4, or 6 since we included breaks after each test, and it stopped after 2. If $value was 11, we would get all the way to \u201c11 is not divisible by 2 through 9.\u201d In this scenario, it is because 11 is a prime number, which by definition is only divisible by itself and 1.<\/p>\n<p>Now let us tweak our switch statement so it tells us all of the values between 2 and 9 that can divide our number without a remainder. First, we will have to start testing the values we could skip earlier. For example, 8 is not divisible by 6 even though both are divisible by 2. Second, we no longer want to stop after one expression is true. To do this, we will get rid of all of our breaks except for the one in the case preceding the default, ensuring that if any of the cases were true, we will not still see our default statement. That gives us:<\/p>\n<pre>switch($value){\r\n\r\ncase ($value % 2 == 0 ):\r\n\r\n<span class=\"CodeIndent1\">echo \"$value is divisible by 2 &lt;br\/&gt;\";<\/span>\r\n\r\ncase ($value % 3 == 0 ):\r\n\r\n<span class=\"CodeIndent1\">echo \"$value is divisible by 3 &lt;br\/&gt;\";<\/span>\r\n\r\ncase ($value % 4 == 0 ):\r\n\r\n<span class=\"CodeIndent1\">echo \"$value is divisible by 4 &lt;br\/&gt;\";<\/span>\r\n\r\ncase ($value % 5 == 0 ):\r\n\r\n<span class=\"CodeIndent1\">echo \"$value is divisible by 5 &lt;br\/&gt;\";<\/span>\r\n\r\ncase ($value % 6 == 0 ):\r\n\r\n<span class=\"CodeIndent1\">echo \"$value is divisible by 6 &lt;br\/&gt;\";<\/span>\r\n\r\ncase ($value % 7 == 0 ):\r\n\r\n<span class=\"CodeIndent1\">echo \"$value is divisible by 7 &lt;br\/&gt;\";<\/span>\r\n\r\ncase ($value % 8 == 0 ):\r\n\r\n<span class=\"CodeIndent1\">echo \"$value is divisible by 8 &lt;br\/&gt;\";<\/span>\r\n\r\ncase ($value % 9 == 0 ):\r\n\r\n<span class=\"CodeIndent1\">echo \"$value is divisible by 9 &lt;br\/&gt;\";<\/span>\r\n\r\n<span class=\"CodeIndent1\">break;<\/span>\r\n\r\ndefault:\r\n\r\n<span class=\"CodeIndent1\">echo \"$value is not divisible by 2 through 9.\";<\/span>\r\n\r\n}<\/pre>\n<p>To repeat our examples using 4, 12, and 11, respectfully we would see the following responses:<\/p>\n<pre>4 is divisible by 2\r\n\r\n4 is divisible by 4\r\n\r\n12 is divisible by 2\r\n\r\n12 is divisible by 3\r\n\r\n12 is divisible by 4\r\n\r\n12 is divisible by 6\r\n\r\n11 is not divisible by 2 through 9<\/pre>\n<\/div>\n\n\t\t\t <section class=\"citations-section\" role=\"contentinfo\">\n\t\t\t <h3>Candela Citations<\/h3>\n\t\t\t\t\t <div>\n\t\t\t\t\t\t <div id=\"citation-list-132\">\n\t\t\t\t\t\t\t <div class=\"licensing\"><div class=\"license-attribution-dropdown-subheading\">CC licensed content, Shared previously<\/div><ul class=\"citation-list\"><li>The Missing Link. <strong>Authored by<\/strong>: Michael Mendez. <strong>Provided by<\/strong>: Open SUNY Textbooks. <strong>Located at<\/strong>: <a target=\"_blank\" href=\"https:\/\/textbooks.opensuny.org\/the-missing-link-an-introduction-to-web-development-and-programming\/\">https:\/\/textbooks.opensuny.org\/the-missing-link-an-introduction-to-web-development-and-programming\/<\/a>. <strong>License<\/strong>: <em><a target=\"_blank\" rel=\"license\" href=\"https:\/\/creativecommons.org\/licenses\/by-nc-sa\/4.0\/\">CC BY-NC-SA: Attribution-NonCommercial-ShareAlike<\/a><\/em><\/li><\/ul><\/div>\n\t\t\t\t\t\t <\/div>\n\t\t\t\t\t <\/div>\n\t\t\t <\/section>","protected":false},"author":311,"menu_order":10,"template":"","meta":{"_candela_citation":"[{\"type\":\"cc\",\"description\":\"The Missing Link\",\"author\":\"Michael Mendez\",\"organization\":\"Open SUNY Textbooks\",\"url\":\"https:\/\/textbooks.opensuny.org\/the-missing-link-an-introduction-to-web-development-and-programming\/\",\"project\":\"\",\"license\":\"cc-by-nc-sa\",\"license_terms\":\"\"}]","CANDELA_OUTCOMES_GUID":"","pb_show_title":"on","pb_short_title":"","pb_subtitle":"","pb_authors":[],"pb_section_license":""},"chapter-type":[],"contributor":[],"license":[],"class_list":["post-132","chapter","type-chapter","status-publish","hentry"],"part":121,"_links":{"self":[{"href":"https:\/\/courses.lumenlearning.com\/suny-the-missing-link-an-introduction-to-web-development-and-programming\/wp-json\/pressbooks\/v2\/chapters\/132","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/courses.lumenlearning.com\/suny-the-missing-link-an-introduction-to-web-development-and-programming\/wp-json\/pressbooks\/v2\/chapters"}],"about":[{"href":"https:\/\/courses.lumenlearning.com\/suny-the-missing-link-an-introduction-to-web-development-and-programming\/wp-json\/wp\/v2\/types\/chapter"}],"author":[{"embeddable":true,"href":"https:\/\/courses.lumenlearning.com\/suny-the-missing-link-an-introduction-to-web-development-and-programming\/wp-json\/wp\/v2\/users\/311"}],"version-history":[{"count":2,"href":"https:\/\/courses.lumenlearning.com\/suny-the-missing-link-an-introduction-to-web-development-and-programming\/wp-json\/pressbooks\/v2\/chapters\/132\/revisions"}],"predecessor-version":[{"id":264,"href":"https:\/\/courses.lumenlearning.com\/suny-the-missing-link-an-introduction-to-web-development-and-programming\/wp-json\/pressbooks\/v2\/chapters\/132\/revisions\/264"}],"part":[{"href":"https:\/\/courses.lumenlearning.com\/suny-the-missing-link-an-introduction-to-web-development-and-programming\/wp-json\/pressbooks\/v2\/parts\/121"}],"metadata":[{"href":"https:\/\/courses.lumenlearning.com\/suny-the-missing-link-an-introduction-to-web-development-and-programming\/wp-json\/pressbooks\/v2\/chapters\/132\/metadata\/"}],"wp:attachment":[{"href":"https:\/\/courses.lumenlearning.com\/suny-the-missing-link-an-introduction-to-web-development-and-programming\/wp-json\/wp\/v2\/media?parent=132"}],"wp:term":[{"taxonomy":"chapter-type","embeddable":true,"href":"https:\/\/courses.lumenlearning.com\/suny-the-missing-link-an-introduction-to-web-development-and-programming\/wp-json\/pressbooks\/v2\/chapter-type?post=132"},{"taxonomy":"contributor","embeddable":true,"href":"https:\/\/courses.lumenlearning.com\/suny-the-missing-link-an-introduction-to-web-development-and-programming\/wp-json\/wp\/v2\/contributor?post=132"},{"taxonomy":"license","embeddable":true,"href":"https:\/\/courses.lumenlearning.com\/suny-the-missing-link-an-introduction-to-web-development-and-programming\/wp-json\/wp\/v2\/license?post=132"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}