PHP: Flow Control

The phrase "Flow Control" might sound weird, as though we're talking about fluids or money. It sounds vague and almost clinical. While "Flow Control" might be something to think about for a variety of other topics, we're going to look at the flow control of data in PHP. As mentioned in the introductory post, lines of PHP code are referred to as statements. When we explored some of the syntax in the last post, the lines of code for declaring variables, performing mathematical operations, or echoing strings were all statements. Below, let's look at the statements we'd use to control the flow of information within a PHP script.

Comparisons

Comparisons in PHP are done with Comparison Operators. I've listed the symbols below.

> Greater than
< Less than
>= Greater than or equal to
<= Less than or equal to
== Equal to
!= Not equal to

For example, below, we're going to see a PHP statement stating that 6 is less than 7.

<?php
     6 < 7;
?>

If Statements

What if we had a PHP form that collected some basic information about an individual and would tell them if they were of legal age to consume alcohol? If someone submits their age and it's over 21, then they can legally consume alcohol, right? Let's look at a few lines of PHP code that could do this?

 <?php
     $age = 27; 
     if($age > 21) {
     echo "You can get a beer!";
          }
?>

Now, this is only an example of an If statement. It puts the age into a variable and checks to see if the value of that variable is greater than the US legal drinking age. If the statement is true, then our code will echo the string "You can get a beer!"

If/Else Statements


This may come as a shock, but If/Else Statements aren't completely different from the If statements we talked about above. Let's add an Else statement to our legal drinking age checker above. 

<?php
     $age = 27;
     if($age> 21) {
          echo "You can get a beer!";
               }
     else {
          echo "Not yet but you'll get there!";
               }
?>

Just as with the above code, if the age is above 21, then the user will get the "You can get a beer!" string echoed. By adding the else statement, we covering if someone's age is less than the legal drinking age, making the if statement false, and echoing back a response to either. 

Switches

Switch statements work a bit like If statements, in the sense that it essentially says "If a condition is true, run this block of code." You'd use a switch statement when you have multiple expressions that depend on the same value. For example, let's say I have a form entry where when someone submits a value of 0,1, or 2, my code will echo back the value. However, if it's above 2, then the code will simply echo back that the value is not 0, 1, or 2.

 <?php
     switch (3) {
        case 0:
            echo 'The value is 0';
            break;
        case 1:
            echo 'The value is 1';
            break;
        case 2:
            echo 'The value is 2';
            break;
        default:
            echo "The value isn't 0, 1 or 2";
    }
    ?>

We'll see that if the value is 0, 1, or 2 then the script will execute the code block that says to echo the value. However, if one of those conditions is not met, then the code will execute a default code block. Basically, if it's this condition, do this. If it's that condition, do that. If it's neither then do this other thing. You'll notice that we have multiple cases but only one default.

You could have empty cases if you'd wanted. Not every case has to have a code block. This is called falling through. For example, let's look at the code below.

<?php
    $i = 7;
    
switch ($i) {
        case 0:
            echo '$i is 0.';
            break;
        case 1:
        case 2:
        case 3:
            echo '$i is 3';
            break;
        case 4:
            echo '$i is 4';
            break;
        case 5:
            echo '$i is somewhere between 1 and 5.';
            break;
        case 6:
        case 7:
            echo '$i is either 6 or 7.';
            break;
        default:
            echo "I don't know how much \$i is.";
    }
    ?>

You'll notice that the empty cases don't have a break statement. Also, notice that the switch statement cases are contained within curly braces.

Comments

Popular posts from this blog

Installing CentOS 7 on a Raspberry Pi 3

Modifying the Zebra F-701 & F-402 pens

How to fix DPM Auto-Protection failures of SQL servers