Control Structures, part I

Now that we’ve taken a look at how to build variables, we’re going to look at how to build the basic logic, called the control structures, that let you manage how your script is executed. Without any control structures, your code will simply execute each line until it reaches the end. But usually you want to change the script execution path based on your data. Control structures give you that flexibility.

The first control structure is called the if statement. All it does is evaluate an expression, and if that expression is true, it invokes the code in its branch. If not, its branch gets skipped.

We define it like this:

if(expr) {
//code to execute
}

The braces define your branch, and they can include as many lines as you need. The parenthetical statement between the word ‘if’ and the opening brace is your test expression.

So, here’s a pretty simple example:

$var1 = true;
if($var1) {
echo ‘I am the true branch!’;
}

{show output}

Because $var1 was true, we went into the branch and saw the somewhat pompous output from it, but if we change $var1 to false, our output is blank:

{modify $var1}

{show output}

Now, if we want, we can define another branch that only gets executed when the expression is false. And we do that with an ‘else’ branch, like so:

if($var1) {
echo ‘I am the true branch!’;
} else {
echo ‘I am the false branch!’;
}

{show output}

This time, since $var1 was still false, we ended up going through the code in the else branch, resulting in the output we see here.

Now sometimes, if and else are insufficient. They only give you the ability to test for two different outcomes. But what if you want to create more than two possible outcomes? If that’s what you want, you can create one or more ‘elseif’ branches. An elseif branch can be created with or without a space between the words ‘else’ and ‘if’ – both work. Just follow it with an expression, the same as declaring an ‘if’ branch, and then define your branch inside braces again:

$var1 = 1;
if($var1 > 0) {
echo ‘$var1 is greater than 0’;
} elseif($var1 == 0) {
echo ‘$var1 is 0’;
} else {
echo ‘$var1 is something else’;
}

{show output}

Now, if both your ‘if’ condition and your ‘elseif’ condition are true, only your ‘if’ branch will be run. PHP stops checking your test expressions after it finds one that evaluates as true. Once it does, it runs that branch and then continues executing below the close of your entire control structure.

As far as the branches go, you’ve got to have an if branch, but the else and else-if branches are entirely optional. And you can have as many elseif branches as you like, but you can only have one else branch connected to an if branch, and it always has to come at the end after any elseif branches you may have defined. Logically this makes sense since else just gets run if no if or elseif branch runs. Another else or a follow-on elseif branch just wouldn’t make any sense.

Now, let’s imagine you have a situation where you’ve got lots of if and elseif branches testing one variable for a whole range of possible values. As in, if $var1 is 1 do this, else if it’s 2 do this, else if it’s 3 do this, and so forth. For this situation, PHP provides us with another control structure called a switch statement.

{show ready-made example script with switch}

In the opening of the switch statement, I tell the script which variable I’m testing the value of. Each of the case statements then provides a value to test that variable against. If the variable’s value matches the value of the one in the case statement, it executes that statement. It continues execution until it encounters a break. If I remove the break,

{remove break from the case that is going to match}

{show output}

it will execute the next case’s instructions even though the variable doesn’t match it’s comparator. Here you can see that that’s happened. Sometimes, I actually want that behavior, for instance when I want the same action to happen for more than one case. But most of the time we want to remember that break.

There’s a couple other important things to keep in mind about the switch statement. First, you can only compare simple types – like floats, strings, and ints. Second, the comparison is loose, so the string ‘123’ matches the int one hundred twenty three. Basically it’s the double-equal sign comparison instead of the triple-equal sign. And third, you can do comparisons with variables and expressions, so long as they evaluate to a simple type.

Alright, now the next thing I want to show you is the default statement:

{show example}

This is basically your else statement with a new name. This statement’s code will get used if none of the cases above it match. You set up a default statement whenever you want to make sure that some code gets executed no matter what:

{show output}

There is one last secret with switch statements, and this is something that not many PHP developers even know about. Those case statements can actually do more than just test whether your test variable equals some value. You can actually do any of the logical tests between a variable and a simple value. This is great when you have, say, a float, and you need to build case statements that test not for a precise value, but for an entire range. To do that, you place your test in parenthesis after the word ‘case’. You can only test the variable you declared in your switch statement, and again, you can only test it against a simple type value, but now you have the freedom to test more than just straight “$a == 2” or something like that. Here’s an example:

{show example}

{show output}

That’s it for part I of control structures. In the next video we’re going to look at loops.

February 09 2010 11:31 pm | Basics and PHP Programming Basics and PHP tutorial scripts

Comments are closed.