Login

PHP Statements and Beginning Loops

We discussed statements briefly in our last article and even got a sneak peek of an IF-statement. Sure I mean, the image was blurry, and the moment it saw us it ran off into the forest. But we saw it. Honest. So in this article, we’re going to take a much closer look at PHP statements and even start learning about loops.

What’s the deal with Bigfoot sightings anyway? And UFO sightings. I’m a firm believer in both mind you (the idea of a 12-foot-tall gorilla as my butler makes me all warm and fuzzy inside), but how come no one can take a video where it isn’t either blurry, the camera isn’t shaking, or the person spends half of the tape filming their own feet?

I mean I can understand the shaky camera part. A giant hairy dude in the middle of a forest watching you might make you tremble in your boots a little. Of course that person would also tremble watching a Robin Williams flick. If you ever need proof of a Sasquatch with a growth disorder, you need look no further. The guy looks like he stapled shag carpeting to his body.

Conditional Statements

Sometimes in a program you want to have a piece of code execute when a condition is met. To do this, you use conditional statements. There are three types of these in PHP. These are the If statement, the Else statement, and the Elseif statement.

{mospagebreak title=If}

People are always asking what if. What if I weren’t the world’s fattest man? What if I poured vodka into that Kool-Aid guy’s body — would he get drunk? I mean technically the dude is already wired. Why else would he just crash through people’s wall and yell his name?

As I said before, we already previewed the IF statement in our previous article. However, I will show it to you one more time here, just as a refresher. The basic idea of an If statement is: If this, do that.

<html>

<body>

<?php

$your_name = "Angelina Jolie";

if ( $your_name == "Angelina Jolie") {

echo ‘Will you marry me?';

}

?>

</body>

</html>

In the above code, we store the name "Angelina Jolie" in a variable. We then say that if the value in $your_name is in fact "Angelina Jolie" print the line:

Will you Marry me?

If the value of $your_name had been any other value, nothing would have happened.

But what good is that program if we don’t get an answer? For that, let’s use the If…Else statement.

{mospagebreak title=Else}

<html>

<body>

<?php

$your_answer = "Yes";

if ( $your_name == "Yes")

{

echo Now I am the both the Happiest and Fattest man alive!';

}

else

{

echo ‘Jennifer Aniston is prettier than you!';

}

echo ‘I’m Brad Pitt’s cousin, Fat Pitt.';

?>

</body>

</html>

Several things to note about the above code: 1) Jennifer Aniston is not hotter than Angelina Jolie, unless Jennifer Aniston or her lawyer are reading this, in which case she is very hot and a great actress to boot 2) If the value in $your_answer is equal to Yes, then the following will print:

Now I am both the Happiest and Fattest man alive!

I’m Brad Pitt’s cousin, Fat Pitt.

If the value in $your_answer had not been Yes, this would have printed:

Jennifer Aniston is prettier than you!

I’m Brad Pitt’s cousin, Fat Pitt.

Note that either way the line: I’m Brad Pitt’s cousin, Fat Pitt prints out. This is because that echo is placed outside of the IF…Else statement and prints regardless of whether the criteria is met.

{mospagebreak title=Elseif}

That sounds like the name of a cow doesn’t it? Good ole’ Elseif just ain’t makin’ as much milk as she used to. But in fact, the Elseif statement does more than give milk. Elseif statements allow you to expand upon the Else statement. Just look at this table and it will be crystal clear:

<html>

<body>

<?php

$marry_who = "James";

if ( $marry_who == "James")

{

echo ‘Now I am the both the Happiest and Fattest man alive!';

}

else if ($marry_who == "Brad")

{

echo ‘Jennifer Aniston is prettier than you!';

else

{

echo ‘Who the hell is he?!?';

}

echo ‘I’m Brad Pitt’s cousin, Fat Pitt.';

?>

</body>

</html>

There, that’s more like it. Now we are forcing the buxom Tomb Raider to choose between me and her lackluster actor boyfriend (I refuse to accept the fact that they are married). Even though in my mind I know who she would pick (and you better too you monkeys), let’s look at the results of this program:

First we started off giving the variable $marry_who the value of "James." Then the program asks who she chose. Clearly, she chose me. And since the value of $marry_who is "James" the following prints out:

Now I am both the Happiest and Fattest man alive!

I’m Brad Pitt’s cousin, Fat Pitt.

Tada. Now if by some crazy luck, the star’s were aligned in Brad Pitt’s favor, it would have printed this:

Jennifer Aniston is prettier than you!

I’m Brad Pitt’s cousin, Fat Pitt.

And lastly, if Angelina has decided handsome, smart, funny, creatively brilliant men (me) and goofy, can’t-act-his-way-out-of-a-paper-bag actors with a yawnable six-pack of abs (Brad Pitt) aren’t her type, and she chooses some other fool, this would be there result:

Who the hell is he?!?

I’m Brad Pitt’s cousin, Fatt Pitt.

Again, since the line about me being Brad Pitt’s cousin is outside of the statement, it will print regardless of the IF…Else…Elseif result.

{mospagebreak title=Here We Go Loop de Loop}

You ever have one of those jobs where you do the same thing over and over all day, every day? Sure you have. Take the job of hamburger flipper. Before becoming the web’s writer extraordinaire, I once worked at a burger restaurant. They sat me through orientation and gave me a giant book to study (which I never did; it was burger flipping, not rocket science). Then they told me my title and I was baffled: Top Bun Operator. Again: Top Bun Operator. No kidding. One guy "operated" the top bun and was responsible for its toppings, while a second guy was the Bottom Bun Operator. Apparently doing the whole bun was much too complicated.

One day, though , I became the Neo of the hamburger world. I was operating not only the top bun and the bottom bun, but the hot dog bun as well. All around me employees were betting their $5.25 an hour salaries and yelling "He’s a madman!"

After that I quit, because let’s face it: I had taken the job as far as it could go.

Can you imagine the poor dunce that gets reprimanded for not knowing how many pickle slices go on the top bun?

Anyway. Loops in PHP (and other programming languages) make the computer do repetitive tasks for you. I mean, that is what computing is all about. The are several types of loops, starting with the For Loop.

{mospagebreak title=For Loop}

Say you want to count to one million, but since you’re too lazy, you decided to watch your computer do it instead. The For loop was designed specifically for slackers like you.

<html>

<body>

<?php

$count_for_me = 0;

for ($i = 0; $i < 1,000,000; $i++)

{

$count_for_me += $count_for_me;

}

?>

</body>

</html>

I recommend changing the $count_for_me part to like 100 before running this program, otherwise you will be sitting there for a while.

Breaking down the Code

The For loop has three parts. The first part is the initializer, which initializes a counter variable. We set it to zero to execute the loop 1,000,000 times. If we set it to 1, it would only go through 999,000 times.

The second part is the conditional part. This is the part where you give the program the criteria of how many times to run. It will keep running until this criteria is met. In this instance while $i is less than a million.

The third and final part of the loop is the loop expression, which tells the program what action to take on the $i counter variable. In this case, we are incrementing it by one.

Well folks, that’s it for this tutorial. We will cover the rest of the loops in the next edition. Till then…