2 Guys, a Mac, and a Website - The Evolution of the Web - PHP Tutorial - Conditionals
October 9th - Hey, happy pantsday.
2 Guys Store

120x60

 Search

 Classic 2 Guys
10 Random Stories:
If Apple Had a Game Division, Oh the Possibilities. . .
My Life In The Corps. Sunday.
Aww, What a Cute Little Searchling
Apple Grounds iPod
A PHP Tutorial - Part 1
And I was going to use UUUUU for my password
2GAMAAW: A Commitment to Apathetic Journalism
Second 2 Guys Podcast for 1/19/05
2 Guys Podcast 6-26-05
If Operating Systems Were Stores

 Comments
yum hot guys - core
You guys are the pants! - PHP WannaBe
Maybe they don't like you - so they sent you defective product. Have yo... - DJLC
A friend of mine had this product, and the antenna portion quickly came... - Cubist
And the other rule of not commenting on your own article!
...
- Jonahan


 Account
Not logged in.

Username:
Password:
Save password
Not registered?




 PHP Tutorial - Conditionals
Ok, time for some more PHP'in! If you've read my previous articles (PHP - The Very Basics
and A PHP Tutorial - Part 1
) you should know that PHP doesn't stand for Parents Helping Parents, and have at least a basic grasp of the language. A machine with PHP installed on it would be good too!

We're working up to some more complex topics here, but we'll take 'em easy and expand on them in future tutorials. In this article we'll be covering conditionals. Conditionals control your PHP scripts flow by telling it to make decisions. If you know other languages you'll be familiar with IF and ELSE and to a lesser extent SWITCH.

IF Statements

The basic structure of an IF statement is this:

if ($x == "15")
// This first line asks "If $x is equal to 15". // If it is, we execute the following code in brackets:
{
echo("x is 15");
}
else
// Otherwise, we execute this code:
{
echo("x is not 15!");
}


Make sense?

IF statements are the bread and butter of PHP scripts, as they can be really useful. Here's an example. Used in conjunction with PHP's built-in DATE function, we can display a different color background on a page depending on whether it's AM or PM. (I'm going to assume you know a little HTML here!) Here's the code:

<?
$timeOfDay = date("A");

if ($timeOfDay == "AM")
echo("<body bgcolor='red'>");
else
echo("<body bgcolor='blue'>");
?>


Try it out - take the above and paste it into a text file and save it as timeOfDay.php (or whatever you like) in your Web Documents folder. Then go to "localhost/timeOfDay.php" in a Web browser. You'll get a blank page with either a red or blue background depending on the time of day on your machine. Pretty simple, but it should give you some idea of all the great dynamic stuff you can do with PHP!

One more thing about IF statements. They can be nested inside each other and many PHP scripts end up having a whole bunch of these nested IF's. This can get messy and somewhat confusing to read, but there are ways of limiting how many IF statements you have to use (SWITCH statements are one way - we'll get to those)

Now for ELSEIF. ELSEIF lets you check for different things in the same structure. Example:

<?
if ($x < "15")
echo("x is less than 15");

elseif ($x == "15")
echo("x is equal to 15");

elseif ($x > "15")
echo("x is greater than 15");
?>


We introduced some new things - "<" means "less than". Likewise ">" means "greater than". These are comparison operators, and there are some more of them, but we won't get into that here. This page has more information about them.

Using ELSEIF's like this works great for our example and for many other things to. However, if you have a whole bunch of ELSEIF's it may just be easier to use a SWITCH statement. So without further ado, our next topic.

Fishsticks

Er...whoops, I meant:

SWITCH Statements

SWITCH statements are kind of like IF statements, but instead of having many, many IF statements to check for lots of different things, you can use SWITCH and save time and typing, and have cleaner code (very important for big projects!).

Here's another example using the DATE function. Depending on the day of the week, we'll print out a certain color for each day:




$timeOfDay = date("l");
// date("D") gives us the actual day it is, i.e. "Monday" or "Tuesday"

switch($timeOfDay)
{
case "Monday":
$timeOfDay = "<body bgcolor='red'>";
break;

case "Tuesday":
$timeOfDay = "<body bgcolor='blue'>";
break;

case "Wednesday":
timeOfDay = "<body bgcolor='green'>";
break;
}

echo $timeOfDay;
?>


So as you can see, we perform a SWITCH operation on one variable, and set up certain cases for it. If a certain case is triggered, we set the background color and then BREAK (or exit the SWITCH structure) and continue on with executing code. I'll leave it up to you to set colors for the rest of the days if you want to.



Well that's it, just a shortie today, as I should probably get back to that little thing called work. I know, I know, my priorities have been slipping lately and I haven't been devoting as much time to 2 Guys as I'd like - but don't worry, the slacking will commence again full-boar very soon!

As always, let me know if you have questions! And no smart guy, questions about PHP, not about monkeys or donuts or monkeys eating donuts, or even ravenous mutant monkey-eating donuts. Geesh. Like I'm some kind of monkey/donut expert. (Ok, I did take that one course in college, but I don't think that qualifies me to answer your questions)

July 18 2003, 10:54 AM EDT, by




Comments:
Jonahan 7/18/03, 11:45 AM EDT
If you're copying the code from the article, it may not work because you'll probably copy some spaces and other artifacts. So you may have to clean it up at little.

We're working on a way to display code w/o having those weird characters, but until then, keep your shorts on!

Jonathan 7/18/03, 7:33 PM EDT
But I like my shorts off!!

Jonahan 7/18/03, 7:58 PM EDT
Well yes, it's always nice to have a nice little breeze for the boys, but sometimes when you have company you just gotta keep 'em on. Sorry!

HTML Samurai 7/23/03, 10:48 AM EDT
Another Example:

if($today == "cold")
  echo("Wear pants!");
elseif($today == "warm")
  echo("Wear shorts!");

Of course you will have to write a script to go get the weather forcast for today, and then set up another script to determine if that forecast is cold or warm, and then pass that variable to the code I listed above...

And you will never have to decide if you should wear pants or shorts for yourself again!

Or just get married and your wife will decide for you!

jonahan 7/23/03, 10:55 AM EDT
Hehehehe....that script would look VERY nice at the top of this page!

Everyone could consult 2 guy's for their pants-wearing decisions. ;)

Which, in my family, the guys always wear the pants, just don't tell the women that!

Jonahan 11/18/03, 5:18 PM EDT
Ok, it's months later and that script is functioning if you create an account and enable 'weather and pants-wearing advice'. Just in case anyone missed it ;)



This article is archived, so you may not comment on it.

(The good news is there's always the shoutbox, the forums or the contact form if you're socially-inclined at the moment!)


iMac G5_468x60
MacMini_02

 Site Links
 Deep Thoughts
Here's a good thing to do if you go to a party and you don't know anybody: First take out the garbage. Then go around and collect any extra garbage that people might have, like a crumpled napkin, and take that out too. Pretty soon people will want to meet the busy garbage guy.

 Around Da Web
iProng:
iPhone steals show at CTIA Wireless 2007
DLO offers dual cover fashion case for iPod
AT&T received 1M inquiries on iPhone
MacDailyNews:
Ars Technica in-depth review: Apple TV ?impressed all those who touched it?
Inside Apple?s Mac OS X 10.5 Leopard Server OS
The chips inside Apple TV
Think Secret:
Adobe Creative Suite 3 pricing revealed
 Olde Stuff
2 Guys Podcast Feed
Greatest American Hero
iAir
Scary Ballmer
Space Game
 We Like:
 • 2 Guys
 • Apple.com

 Side Projects
Jonahan
  • JediPoker.net
  • Jonahan.com
  • iProng
  • MacProng
iKen
  • MacIdiot
Jedbeck
  • Jedbeck.com
J.P.
  • Baby Ashley Project