Blake O'Hare .com

PHP Tutorial Part 3 - Manipulating Variables and Program Flow

Variables aren't only defined by passing parameters to the URL. You can define them within your code. Consider the following example...
<html><body>
<p>This is HTML code</p>
<?
  $c = $a * $b;
  echo "<p>";
  echo $c;
  echo "</p>";
?>
<p>This is HTML code again</p>
</body></html>


This does the same thing that the example in the previous section did, but this time we've stored the result of a * b in a variable called c. The equals sign should be thought of as a ← instead as you are evaluating the code on the right side of the equals sign and storing it in the variable on the left side. It may seem like extraneous code, but this does provide benefits. Such as being able to display the result multiple times without recalculating the value of a * b.

It is a rather canonical example, but after we get our hands more dirty, you'll find yourself making variables all over the place.

You probably already assumed this, but addition, subtraction, division, and parenthesis are also supported.

Program Flow

The PHP script, by default, starts from the beginning of the file and executes each line of code one by one. Once it's finished with one line, it moves to the next. Each line is terminated by a semicolon.

Sometimes you'll want it to skip certain lines or do other lines multiple times. There are a few mechanisms to do this.

The if Statement

The if statement allows you to skip lines of code if a condition is not met. Let's modify our multiplication script from above...

<html><body>
<p>This is HTML code</p>
<?
  $c = $a * $b;
  echo "<p>";
  echo $c;
  echo "</p>";
  if ($c > 1000000)
  {
    echo "<p>That's a big number!</p>";
  }
?>
<p>This is HTML code again</p>
</body></html>


In this example, we want our code to display an additional comment if the result of multiplying a and b is larger than 1,000,000. The if statement is literally just the word "if" followed by parenthesis with some sort of statement that can either be true or false. If it is true, then the lines of code in the curly braces are executed. If not, then they are skipped.

If there are no curly braces after the if statement, then it will be assumed that the next line of code is the only conditional statement. This should be avoided as it is not good coding style and sometimes leads to ambiguities in your code.

The else Statement

The else statement can be added to the end of an if statement. Code enclosed in curly braces after an else statement are executed if the if statement condition is not true...

<html><body>
<p>This is HTML code</p>
<?
  $c = $a * $b;
  echo "<p>";
  echo $c;
  echo "</p>";
  if ($c > 1000000)
  {
    echo "<p>That's a big number!</p>";
  }
  else
  {
    echo "<p>That's not quite as big...</p>";
  }
?>
<p>This is HTML code again</p>
</body></html>


Else statements can only exist after if statements. If they are written anywhere else, the server will throw an error.

The while Loop

Think of while statements as if statements that keep repeating until the condition is finally no longer true. I'm tired of the multiplication example. Let's use something else...

<html><body>
<?
  $x = 1;
  while ($x < 100)
  {
    echo "<p>Meow!</p>";
    $x = $x + 1;
  }
?>
</body></html>


In this example, we start out a variable called x with the value of 1. Then we have a while statement that tests to see if x is less than 100. If it is, it will execute the code in the curly braces over and over until x is finally not less than 100. Inside the conditional code, we add a Meow to the output HTML and increment x by 1. The word Meow will appear in our HTML 99 times when this is run.

One thing I find that trips up first time programmers is the whole $x = $x + 1; thing since they are used to solving variables in algebra. Remember that they're only called variables but are not like their algebraic cousins. PHP evaluates the expression on the right side of the equals sign and stores that result into the variable on the left side. Even if the variable on the left is used on the right. This is a purely procedural process. No mathematical solving.

LOOPS ARE DANGEROUS! What would happen if we forgot to write the $x = $x + 1; line in our code? The while statement would run over and over again forever! Depending on the server configuration, this could cause any number of ill side effects. Usually you just see a "500: Internal Server Error" page displayed. You don't want that. Be careful.

The for Loop

The for loop basically is a syntactical variation on the while loop. Consider it shorthand for the while loop. Here is the same example from above written with a for loop instead of a while loop.

<html><body>
<?
  for ($x = 1; $x < 100; $x = $x + 1)
  {
    echo "<p>Meow!</p>";
  }
?>
</body></html>

Instead of setting up the x variable outside of the loop, incrementing x inside, and checking the condition in the parenthesis, the for loop combines all 3 inside the parenthesis. This is often convenient since you will be less likely to forget a key line of code and accidentally write an infinite loop.

Next: Strings
Back to Tutorial Overview