Section 3: Scripting Languages

Chapter 31: Structures

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.

If

“If” is perhaps the simplest test we can apply in logic. In programming, the “then” side is implied in the code contained in the definition of our statement—If [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.

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.

  1. if(“hot” == $coffee){
  1. drink($coffee);
  1. }

Additional notes

Remember that = is an assignment operation! == or === must be used when testing if both sides of the operand are equivalent.

Else

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:

  1. if(drink($coffee)==’full’){
  1. cleanUp();
  1. }
  1. else{ //We want more!
  1. drink($coffee);
  1. }

elseIf

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:

  1. if ($a > $b) {
  1. echo “a is bigger than b”;
  1. } elseif ($a == $b) {
  1. echo “a is equal to b”;
  1. } else {
  1. echo “a is smaller than b”;
  1. }

While

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.

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.

While loops are very similar to If in terms of structure. Here we will pretend $ourValue is a 4:

  1. $ourValue=4;
  1. while($ourValue!=6){
  1. echo “Wait for it…<br/>”;
  1. $ourValue++;
  1. }
  1. echo “6!”;
  1. Wait for it…
  1. Wait for it…
  1. 6!

Let us try one that counts for us:

  1. $ourValue=4;
  1. while($ourValue!=10){
  1. echo “$ourValue,” ;
  1. $ourValue++;
  1. }
  • 4, 5, 6, 7, 8, 9,

To see a broken While statement in action, change $ourValue to 11 and refresh your page!

Do While

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:

  1. do {
  1. echo $i;
  1. } while ($i > 0);
  1. echo “<br/> done”;
  • 0
  • done
  1. while ($i>0){
  1. echo $i;
  1. }
  1. echo “<br/> done”;
  • done

For

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’s 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:

  1. $x=5;
  1. for($x;$x<10;$x+=2){
  1. echo “$x is less than 10 <br/>”;
  1. }
  1. echo “$x is greater than 10!”;
  1. 5 is less than 10
  1. 7 is less than 10
  1. 9 is less than 10
  1. 11 is greater than 10!

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:

  1. $x=5;
  1. for($x;$x<10;$x+=2){
  1. echo “$x is less than 10 <br/>”;
  1. if($x==7)$x=11;
  1. }
  1. echo “$x is greater than 10!”;
  1. 5 is less than 10
  1. 7 is less than 10
  1. 11 is greater than 10!

Foreach

Although we took a brief look at Foreach when we reviewed pseudo-code, 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.

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’s 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 (&) before the variable name, which instructs the function to apply changes directly to our original array.

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().

  1. $array = array(1, 2, 3, 4, 5);
  1. foreach($array as $number){
  1. echo “Our value is $number <br />”;
  1. }
  1. Our value is 1
  1. Our value is 2
  1. Our value is 3
  1. Our value is 4
  1. Our value is 5

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:

  1. $array = array(“Mike”=>42, “Frank”=>38, “Anne”=>28);
  1. foreach($array as $key=>$value){
  1. echo “$key is $value years old.<br />”;
  1. }
  1. Mike is 42 years old.
  1. Frank is 38 years old.
  1. Anne is 28 years old.

Finally, we will take a look at applying our changes back to the array, multiplying our original array’s values by 2:

  1. $array = array(1, 2, 3, 4, 5);
  1. foreach($array as &$number){
  1. $number = $number * 2;
  1. }
  1. print_r($array);
  1. Array(
  1. 0 => 2
  1. 1 => 4
  1. 2 => 6
  1. 3 => 8
  1. 4 => 10
  1. )

Switch

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.

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:

  1. switch($value){
  1. case ($value % 2 == 0 ):
  1. case ($value % 3 == 0 ):
  1. case ($value % 5 == 0 ):
  1. case ($value % 7 == 0 ):
  1. default:
  1. }

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:

  1. Case 2:

Or, if we wanted to test if the value is the WORD two, we would use:

  1. Case “two”:

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 “break;“ 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 “break;” depending on the extra conditions we are testing.

You will also notice the “default:” that snuck in at the bottom. The default case must be last, but is optional, and gives us a “catch all” action in the event that none of our cases were met. We do not need a “break;” after our default, since it will always be the last case in our switch.

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:

  1. switch($value){
  1. case ($value % 2 == 0 ):
  1. echo “$value is divisible by 2 <br/>”;
  1. break;
  1. case ($value % 3 == 0 ):
  1. echo “$value is divisible by 3 <br/>”;
  1. break;
  1. case ($value % 5 == 0 ):
  1. echo “$value is divisible by 5 <br/>”;
  1. break;
  1. case ($value % 7 == 0 ):
  1. echo “$value is divisible by 7 <br/>”;
  1. break;
  1. default:
  1. echo “$value is not divisible by 2 through 9.”;
  1. }

Additional notes

This does not mean our switch will find all prime numbers! Prime numbers have to be tested against the range 2 to n1/2 to ensure there are no dividends.

With this example, if $value was 4, we would get “4 is divisible by 2.” If $value was 12, we would get “12 is divisible by 2” 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 “11 is not divisible by 2 through 9.” In this scenario, it is because 11 is a prime number, which by definition is only divisible by itself and 1.

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:

  1. switch($value){
  1. case ($value % 2 == 0 ):
  1. echo “$value is divisible by 2 <br/>”;
  1. case ($value % 3 == 0 ):
  1. echo “$value is divisible by 3 <br/>”;
  1. case ($value % 4 == 0 ):
  1. echo “$value is divisible by 4 <br/>”;
  1. case ($value % 5 == 0 ):
  1. echo “$value is divisible by 5 <br/>”;
  1. case ($value % 6 == 0 ):
  1. echo “$value is divisible by 6 <br/>”;
  1. case ($value % 7 == 0 ):
  1. echo “$value is divisible by 7 <br/>”;
  1. case ($value % 8 == 0 ):
  1. echo “$value is divisible by 8 <br/>”;
  1. case ($value % 9 == 0 ):
  1. echo “$value is divisible by 9 <br/>”;
  1. break;
  1. default:
  1. echo “$value is not divisible by 2 through 9.”;
  1. }

To repeat our examples using 4, 12, and 11, respectfully we would see the following responses:

  1. 4 is divisible by 2
  1. 4 is divisible by 4
  1. 12 is divisible by 2
  1. 12 is divisible by 3
  1. 12 is divisible by 4
  1. 12 is divisible by 6
  1. 11 is not divisible by 2 through 9

License

Icon for the Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License

Chapter 31: Structures by Michael Mendez is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License, except where otherwise noted.