MySQL & MSSQL Tutorial: Combine Multiple Rows Into A Single, Comma Delineated List

Leave a comment Standard

In MySQL with PHP:

<?
$delimiter = ", ";
$count = 0;

$loop = mysql_query("SELECT S.suffix FROM people P
          INNER JOIN surnames S on P.suffix_id = S.id")
          or die ('cannot select merged data');

while ($row = mysql_fetch_array($loop))
{
    $merged_data .= $row['suffix'];

    if ($count+1 < mysql_num_rows($loop))
       $merged_data .= $delimiter;

    $count++;
}

echo $merged_data;
?>

In MSSQL:

declare @merged_data varchar(2000), @delimiter char(1)  --declare your variable

set @delimiter = ', ' --whatever you want to separate the field values

--join two tables and select the fields you want to merge into a single field
 select @merged_data = isNull(@merged_data + @delimiter, '') + S.suffix
 from people P inner join surnames S on P.suffix_id = S.id

 select @merged_data

Video Games Boost Child Development & Health Presented By the Wilson Center

Leave a comment Standard

I got an email about this just this morning. Sounds interesting. It will focus on how video games and emerging media can be beneficial to children and teens. It’s a nice change from all the negative press video games get from educators.

Live Webcast starts 12noon EST on June 23rd, 2009 and runs till about 2pm EST. You can find the link to listen in here. Or you can download the whitepaper and/or the presentation.

HTML Tutorial: A Crash Course In Tables

Leave a comment Standard

It amazes me how many people proclaim to be programmers but they can’t even write simple HTML code. Sooo, this post is in response to all the would be web developers out there who don’t really know what in the hell they’re doing when it comes to tables.

FAQ: Why is it important to properly format and nest your tables?

Different browsers behave differently for code that is written improperly. To help ensure your table displays the way you intended it to always use correct nesting and ensure you have matching tags.

A Simple Table

<table>
   <tr>
     <td>hello there</td>
     <td>bye for now</td>
   </tr>
</table>

Simple Table (Visual)

hello there bye for now

This table has one row and two columns. A table always starts and ends with a <table> and </table> tag. If you forget to put the end </table> tag and start another table immediately following you’ll sometimes find that the browser will close your first table for you — but not always! It’s not safe to assume that your browser is smart enough to fix your HTML mistakes for you.

Now a table isn’t limited to a number of rows or columns but I find many people don’t know how to span rows or columns appropriately.

Spanning A Column

<table>
  <tr>
   <td>hello there</td>
   <td>bye for now</td>
  </tr>
  <tr>
   <td colspan="2">huh?</td>
  </tr>
</table>

Spanning A Column (Visual)

hello there bye for now
huh?

When you span a column the cell takes up the space of both columns. A lot of time I see people put in extra <td> and </td> tags even when they have a colspan set to the appropriate number. The colspan always equals the number of columns you want to span (if that isn’t obvious enough). There’s never any need to put a colspan of 1 because that’s the same thing as having a <td> and </td> without a colspan attribute.

Spanning A Row

<table>
  <tr>
   <td rowspan="2">Hey there</td>
   <td>bye for now</td>
  </tr>
  <tr>
    <td>huh?</td>
  </tr>
</table>

Spanning A Row (Visual)

Hey there bye for now
huh?

This does the opposite thing that a colspan does. Your table cells are now merged down instead of merged across. When you do a row span there is no need to include a <td> and </td> for the row you’re merging over. If you do you’ll end up with strange results and a misshappen table. The cell that has the rowspan attribute on it is always the one that merges into the cell that would have been beneath it.

Table Attributes

Here’s a list of attributes you can assign to the table tags. Ironically enough I’ve decided to put these in a table.

Attribute Description Example
Border this sets an outline around the tables <table border=”1″>
Cell Padding this puts space around the content inside of a table cell <table cellpadding=”2″>
Cell Spacing this puts space between the td cells <table cellpadding=”0″>
Width set the exact or relative width of the entire table or cell <td width=”20%”>
Height set the exact or relative height of the entire table or cell <td height=”22″>
Align sets the text alignment inside of the cell (left, right, middle, justify) <td align=”right”>
Valign sets the vertical alignment of the td cell (top, bottom, middle, abstop, absmiddle, absbottom, baseline) <td valign=”middle”>
Class used to setup a style on the cell <td class=”darkblue”>
Background set the background color of the table <table background=”#000000″>
Foreground set the text color in the table <td foreground=”#FFFFFF”>

PHP Tutorial: Learning PHP (Part 3)

Comment 1 Standard

In Part 2 I covered else-if statements and more complicated if statements. One of the other concepts that’s essential to learning any kind of programming is loops. There are three different types of loops (while loop, for loop, and do while loop), but I’m only going to cover two of them here.

While Loops

The concepts of loops is extremely simple. While some condition is true you’re going to run the same bit of code again and again and again until the condition is no longer true anymore. Right away you can probably see the problem with loops: if the condition never becomes false then the loop with go on endlessly. This is called an infinite loop. Infinite loops should be avoided, they eat up your computer’s resources and can even crash your computer completely. In PHP the worst that will happen is that your browser gets a timeout error or crashes. Not the end of the world, but annoying at the same time.

Usually when I explain this example to people I introduce them to Polly, the brightly colored parrot that looooves to repeat everything you say. So, let’s pretend that we walk up to Polly’s cage with a cracker in our hand. Polly, once she sees a cracker, won’t stop begging for one until you give it to her.

<?php

$treat = “cracker”;

while ($treat = “cracker”)
{

echo “Polly want’s a cracker!<br>”;

$treat = “none”; //you give Polly the cracker after she asked for it so now you don’t have a treat anymore

} //once a loop reaches this part it goes back to the beginning and runs the code inside of these brackets all over again

?>

Run this code and see what it does. Polly asks for the cracker, and because you have a fondness for Polly you decide to give it to her. Now lets say you walk up with 5 crackers. Polly seems them and starts begging again.

<?php

$crackers = 5;

while ($crackers > 0) //as long as you have a cracker
{
echo “Polly wants a cracker!<br>”;

$crackers–; //you give Polly a cracker, so the number of crackers you have goes down by one
}

?>

Now run the script and see what happens. Suddenly Polly is asking you for crackers more then once. In fact, she asks you for a cracker 5 times until you don’t have any more crackers left.

What happens if you take out $crackers–;? Would Polly still ask you for crackers? How many times will she ask you?

The syntax for a loop is really simple. As long as the condition inside of it is true, the loop will go through. Now look at some of these examples –NONE of them work correctly. Can you figure out why?

<?php

$crackers = 0;

while ($crackers > 0)
{
echo “Polly wants a cracker!<br>”;
$crackers–;
}

?>

In this case, Polly won’t ask for a cracker because you don’t have any.

<?php

$crackers = 4;

while ($crackers > 5)
{
echo “Polly wants a cracker!<br>”;
$crackers–;
}
?>

In this case, Polly won’t ask you for a cracker because you don’t have more then 5 of them.

<?php

$crackers = 5;

while ($crackers > 0)
{
echo “Polly wants a cracker!<br>”;
$crackers++;
}
?>

In this case, Polly will ask for a cracker forever because each time she asks, you suddenly end up with another cracker out of thin air! See how it says $cracker++? That means increase the number of crackers you have by one each time.

<?php

$crackers = 3;

while ($crackers > 0)
{
echo “Polly wants a cracker!<br>”;
}
?>

Can you sport the problem with this one? This will be an infinite loop because you always have 5 crackers and they never decrease as Polly asks for one.

Now, just because this loop looks really simple doesn’t mean loops can’t have other things php syntax inside of them. Take this for example:

<?php

$crackers = 5;

$mood = “friendly”;

while ($crackers > 0)
{
echo “Polly wants a cracker!<br>”;

if ($mood == “friendly”) //you’re in a good mood today
{
$crackers–; //you give Polly a cracker
}
else
{
echo “Sorry Polly, no crackers for you today!”;
$crackers = 0; //you eat all your crackers yourself
}

}
?>

In this case you only give Polly your crackers if you’re in a friendly mood. Try it out and see what happens when your mood changes.

Programming Challenge! Lets say one day you walk up to Polly and you have 5 crackers. You’re in a good mood so you decide to give her one. Unfortunately Polly isn’t in such a good mood and she bites your finger after the first cracker you give her. Once that happens your mood changes to angry and you eat 2 of the crackers yourself. But Polly is persistent and after a change of heart you give her the remaining 2 crackers. Can you write a while loop to reflect this?

For Loops

Another type of loop is called a for loop. Usually you use a for loop when you have a set amount of times you want to repeat a portion of code. For instance, lets say you run a pet shop and you know you have 23 cages needing cleaning every day. In this case we know you’ll always have to clean a cage 23 times. These are the cases where its appropriate to use a for loop. When you’re uncertain how often you’ll need to repeat a portion of code its more appropriate to use a while loop. Now lets take a look:

<?php

for ($cleancages= 1; $cleancages < 23; $cleancages++)
{
echo “I cleaned cage number $cleancages<br>”;
}

?>

Try running this script. You’ll see that you cleaned cages 1-23. A for loop works exactly like a while loop with a little difference.

$cleancages = 1;

The variable above, at the beginning of the for loop, sets $cleancages to equal one.

$cleancages < 23

Here the for loop checks to see if the number of clean cages is less then 23. As soon as clean cages goes to 24 the for loop stops, because there are only 23 cages and 24 is greater then 23. This part of the for loop is exactly the same as what you would put inside of a while loop.

$cleancages++

One thing to notice is that there is NO SEMICOLON at the end of this. That is how the for loop knows you want to loop for a certain number of times. This part of the for loop is the same as when we were doing $crackers++; or $crackers–; It tells the for loop that ever time it runs it should increase the variable $cleancages by a value of 1.

So, there you have it. Think you can do this?

Programming Challenge! Write a script that has two variables, $forloop and $whileloop. Make it so that if there is a value in the $forloop variable that the script will run a for loop to clean a number of cages. However, if there is a value in the $whileloop variable the script should run a while loop to clean the cages instead. Once you have that done, see if you can make it so the script will run both a for loop and a while loop if there is a value in either of the two variables.

All done? Now try your hand at Pits of Doom — a free php/mysql online game tutorial that will walk you through designing your own database driven game from start to finish.

PHP Tutorial: Learning PHP (Part 2)

Comments 2 Standard

In Part 1, I talked about requirements for php, creating a file, variables, and if-else statements. Now on to harder concepts.

Else-If Statements

Before we covered the if-else statement. In an if-else statement if something is true take some action, otherwise do something else. The else-if statement builds on the power of an if-else. Look at this example:

<?php

$money = 20;

$bread = 5;

$candy = 5;

$cake = 15;

if ($money – $bread – $candy – $cake >= 0)
{
echo “I can afford to buy bread, candy and cake<br>”;
}
else if ($money – $cake – $candy == 15)
{
echo “I can afford cake and candy with 15 dollars left over<br>”;
}
else if ($money -$cake – $candy == 10)
{
echo “I can afford cake and candy with 10 dollars left over<br>”;
}
else if ($money – $cake – $candy == 5)
{
echo “I can afford cake and candy with 5 dollars left over<br>”;
}
else
{
echo “I shouldn’t buy cake and candy, it’s not good for me<br>”;
}

?>

What do you think will show up on the screen when you run this script? If you guessed it will tell you that you have 10 dollars left over you are correct. An else-if statement lets you add conditions to the else part of the statement. Now what happens if you change price of candy to $8? Play around with this script. Add more if-else statements and see what it does.

Important! You can only have one else statement at the end of if and if-else statements. If you try to add multiple else statements php will give you an error. Why do you think that is?

Do you think it make sense to have something like this:

<?php

$money = 5;

$candy = 5;

$bread = 10;

if ($money – $bread >= 0)
{
echo “I can afford bread”;
}
else
{
echo “I can’t afford bread”;
}
else
{
echo “I really really can’t afford bread”;
}

?>

The answer is no. An else statement marks the end of all the things you’re testing conditionally. Therefore there’s no reason to have multiple else statements at the end of an if or if-else statements. Think of it as trying to put two periods at the end of the sentance. You only need one period so having another period is considered to be an error.

Complex If Statements

Let’s put your knowledge to the test! Create a php file called bookstore.php. Put this inside of it:

<?php

//**************
//Filename: bookstore.php
//From: blog.design1online.com
//Modified By: (your name)
//Modified On: (todays date)
//Info: learning php tutorial part 2
// – this is called a header. If you make a commented header at the top of every
// php file to help you remember what you did and why.
//About: You walk into a bookstore and find three books you want. Since you like all the
//booksequally well you want to buy as many books as you can afford.
//**************

$book1 = “blue”;
$cost1 = 10;

$book2 = “green”;
$cost2 = 5;

$book3 = “red”;
$cost3 = 15;

$book4 = “yellow”;
$cost4 = 5;

$money = 25;

?>

Try it! Can you write a bunch of if-else statements that show which books you would buy without going over how much money you have? Here’s a hint, you may have to make more variables to help you figure it out. Once you’ve come up with an answer view the php page to see answer you get. It should tell you to buy hree books, either green red and yellow or yellow green and blue.

Think you have the answer? Even if you don’t its okay. Here’s a solution I came up with.

<?php

//**************
//Filename: bookstore.php
//From: blog.design1online.com
//Modified By: (your name)
//Modified On: (todays date)
//Info: learning php tutorial part 2
//About: This is my solution. Your script probably doesn’t look the same but it should
//give you the same results. That’s okay! It’s possible to get the correct answer in
//different ways.
//**************

$book1 = “blue”;
$cost1 = 10;
$book2 = “green”;
$cost2 = 5;

$book3 = “red”;
$cost3 = 15;

$book4 = “yellow”;
$cost4 = 5;
$money = 25;

$currentmoney = $money;
$totalbooks = 0;

if ($money – $cost1 – $cost2 – $cost3 – $cost4 >= 0) //trying buying all four books
{
echo “I can afford the ” . $book1 . ” book for ” . $cost1 . “<br>”;
echo “I can afford the ” . $book2 . ” book for ” . $cost2 . “<br>”;
echo “I can afford the ” . $book3 . ” book for ” . $cost3 . “<br>”;
echo “I can afford the ” . $book4 . ” book for ” . $cost4 . “<br>”;

$amount = $money – $cost1 – $cost2 – $cost3 – $cost4;

echo “Which leaves me with ” . $amount . ” dollars left over”;
}

if ($money – $cost1 – $cost2 – $cost3 >= 0) //buying three books at at ime
{
echo “I can afford the ” . $book1 . ” book for ” . $cost1 . “<br>”;
echo “I can afford the ” . $book2 . ” book for ” . $cost2 . “<br>”;
echo “I can afford the ” . $book3 . ” book for ” . $cost3 . “<br>”;

$amount = $money – $cost1 – $cost2 – $cost3;

echo “Which leaves me with ” . $amount . ” dollars left over”;
}

if ($money – $cost2 – $cost3 – $cost4 >= 0)
{
echo “I can afford the ” . $book2 . ” book for ” . $cost2 . “<br>”;
echo “I can afford the ” . $book3 . ” book for ” . $cost3 . “<br>”;
echo “I can afford the ” . $book4 . ” book for ” . $cost4 . “<br>”;

$amount = $money – $cost2 – $cost3 – $cost4;

echo “Which leaves me with ” . $amount . ” dollars left over”;
}

if ($money – $cost1 – $cost3 – $cost4 >= 0)
{
echo “I can afford the ” . $book1 . ” book for ” . $cost1 . “<br>”;
echo “I can afford the ” . $book3 . ” book for ” . $cost3 . “<br>”;
echo “I can afford the ” . $book4 . ” book for ” . $cost4 . “<br>”;

$amount = $money – $cost1 – $cost3 – $cost4;

echo “Which leaves me with ” . $amount . ” dollars left over”;
}

if ($money – $cost1 – $cost2 – $cost4 >= 0)
{
echo “I can afford the ” . $book1 . ” book for ” . $cost1 . “<br>”;
echo “I can afford the ” . $book2 . ” book for ” . $cost2 . “<br>”;
echo “I can afford the ” . $book4 . ” book for ” . $cost4 . “<br>”;

$amount = $money – $cost1 – $cost2 – $cost4;

echo “Which leaves me with ” . $amount . ” dollars left over”;
}

if ($money – $cost1 – $cost2) //trying buying two books at a time
{
echo “I can afford the ” . $book1 . ” book for ” . $cost1 . “<br>”;
echo “I can afford the ” . $book2 . ” book for ” . $cost2 . “<br>”;

$amount = $money – $cost1 – $cost2;

echo “Which leaves me with ” . $amount . ” dollars left over”;
}

if ($money – $cost1 – $cost3)
{
echo “I can afford the ” . $book1 . ” book for ” . $cost1 . “<br>”;
echo “I can afford the ” . $book3 . ” book for ” . $cost3 . “<br>”;
$amount = $money – $cost1 – $cost3;

echo “Which leaves me with ” . $amount . ” dollars left over”;
}

if ($money – $cost1 – $cost4)
{
echo “I can afford the ” . $book1 . ” book for ” . $cost1 . “<br>”;
echo “I can afford the ” . $book4 . ” book for ” . $cost4 . “<br>”;
$amount = $money – $cost1 – $cost4;

echo “Which leaves me with ” . $amount . ” dollars left over”;
}

if ($money – $cost2 – $cost3) //trying buying two books at a time
{
echo “I can afford the ” . $book1 . ” book for ” . $cost2 . “<br>”;
echo “I can afford the ” . $book2 . ” book for ” . $cost3 . “<br>”;
$amount = $money – $cost2 – $cost3;

echo “Which leaves me with ” . $amount . ” dollars left over”;
}

if ($money – $cost2 – $cost4) //trying buying two books at a time
{
echo “I can afford the ” . $book2 . ” book for ” . $cost2 . “<br>”;
echo “I can afford the ” . $book4. ” book for ” . $cost4 . “<br>”;
$amount = $money – $cost2 – $cost4;

echo “Which leaves me with ” . $amount . ” dollars left over”;
}

if ($money – $cost3 – $cost4) //trying buying two books at a time
{
echo “I can afford the ” . $book3 . ” book for ” . $cost3 . “<br>”;
echo “I can afford the ” . $book4 . ” book for ” . $cost4 . “<br>”;
$amount = $money – $cost3 – $cost4;

echo “Which leaves me with ” . $amount . ” dollars left over”;
}

if ($money – $cost1) //trying buying one book only
{
echo “I can afford the ” . $book1 . ” book for ” . $cost1 . “<br>”;
$amount = $money – $cost1;

echo “Which leaves me with ” . $amount . ” dollars left over”;
}

if ($money – $cost2)
{
echo “I can afford the ” . $book2 . ” book for ” . $cost2 . “<br>”;
$amount = $money – $cost2;

echo “Which leaves me with ” . $amount . ” dollars left over”;
}

if ($money – $cost3)
{
echo “I can afford the ” . $book3 . ” book for ” . $cost3 . “<br>”;
$amount = $money – $cost3;

echo “Which leaves me with ” . $amount . ” dollars left over”;
}

if ($money – $cost4)
{
echo “I can afford the ” . $book4 . ” book for ” . $cost4 . “<br>”;
$amount = $money – $cost4;

echo “Which leaves me with ” . $amount . ” dollars left over”;
}

?> Phew! That was a lot of code! Try it and see what it does if you couldn’t get your own version working. Change the prices of the books. Does that change your result?

Programming Challenge: modify bookstore.php so it always returns the most books you can buy and you still have some money left over. For instance, in the example above you would pick books yellow, green, and blue because you’d still have $5 left over.

Continue on to Part 3 of this tutorial.

PHP Tutorial: Learning PHP (Part 1)

Comments 2 Standard

When the Internet exploded in the .com age people were just starting to realize the potential of online programming languages. PHP, or Hypertext Preprocessing, is one of my favorite programming languages. One of the best things about PHP is that it’s easy enough for people to learn even if they’ve had little to no prior programming experience. It’s weakly typed so beginners don’t have the additional complexity of trying to put strings in integers, or understand the difference between a double and float.

So here’s a little PHP lesson for anyone whose decided they’d like to try their hand at it.

Requirements
In order for PHP to work you need a few things.

  1. A place to host all of your files while you test it out. Can’t afford hosting? No problem check out XAMPP!
  2. Your hosting provider needs to have PHP version 4.0 or later installed.
  3. Access to an FTP program, or control panel so you can upload your files to your web space.
  4. Knowledge of basic HTML concepts is a plus. If you want to brush up on your HTML skills try http://www.lissaexplains.com

Creating a PHP File
This is super easy.

  1. Navigate to a place on your computer where you’d like to keep all your PHP files.
  2. Create a new folder for all of your files. Call it something like PHP tutorial, or something you can remember.
  3. Right click and select create a new text file. If you don’t see that option you can always go to any notepad or wordpad program and when it opens you’ll have a blank file in front of you. Either way you should end up with a blank window in some kind of text editor program.
  4. Go to the file menu at the top of the text editor and choose file > save as. You can give the file any name you want, just make sure you append .php to the end of the file. For instance, you might put in demofile.php
  5. Save your file in the folder you created for it. You should see that the file now has a .php extension. That’s exactly what you want.

Your First Script

You can use PHP to tell you all sorts of interesting things about the server you are using with your hosting provider. This comes in handy as you continue to work with PHP and need to know which features are installed, or what software it’s running on.

    1. Open the file you created earlier. Inside it you want to write:
<?php

//this will display information about the PHP setup on your web space

phpinfo();

?>
  1. After you’ve entered this in your file go ahead and save it.
  2. Upload the file to your web space.
  3. Navigate to that file on your web space and view it. You should see something that looks kind of like this:

 

phpinfo()

This is all generated by what you wrote in the file. Now to explain a little bit about what we did.

PHP has start tags and end tags, a lot like HTML. With HTML you can make text change color, fonts get bigger and smaller, etc. To do this you use tags. For instance, to make something bold you would write <b>text that you want bold</b> in your HTML file. PHP works on a similar idea. When you start writing PHP code you have to use <?php and when you’re done using PHP you end the tag with ?>.

Sounds easy enough.

Now lets talk about the comment you see. A comment is something that you can write in your PHP code that allows you to document what you did, or remind yourself of something. Because PHP is server-side only, everything you write in your PHP file is invisible to other users.

Try this! Right click on the screen with the purple PHP information and select view source code. What do you see? You’ll notice you don’t see any of the code you wrote into your file. That’s what makes PHP server-side only. Some other online languages, like Javascript for instance, are not server-side only. If you were to right click on a file with Javascript in it and select view source code you could see all of the Javascript code you wrote in the file.

Comments are special. Unlike other code, a comment doesn’t actually do anything. You can and should use comments to explain what you are doing in your code and why you’re doing it. The more you comment your code the more it can help you later. Let’s say you start working on a piece of code, and then get busy doing something else for a few weeks. When you come back to the piece of code your comments can refresh your memory and help you continue where you left off.

Finally, we’ll look at the last thing in the file, phpinfo(). A lot of programming languages use functions and PHP is one of them. phpinfo() is a function defined by the PHP language which displays information about the version of PHP you are using, any add on capabilities, and some information about the server you’re on. But this isn’t a tutorial about functions, so we’ll leave the explaining there and move on to more of the basics.

One other thing you may notice is the semicolon ; after the phpinfo() function. A semicolon in PHP is a lot like a period at the end of a sentence. A semicolon tells PHP that you’ve come to the end of a line in your code.

Did You Know? One of the most common problems you’ll run into as you learn how to program in PHP are syntax errors, like forgetting to put a semicolon at the end of a line of code. There are a lot of other syntax errors you’ll learn about as you keep programming. But don’t be discouraged! Even experienced programmers get syntax errors, they just have more familiarity with errors which helps them fix them faster.

Variables

The easiest way to think about a variable is to imagine a container. A variable, just like a container, can store things. However, a variable, just like a container, has some limits as well.

A variable is used to hold information. Sometimes the variable has values in it, like a container might have some cookies inside, and sometimes it’s empty. We say that an empty variable has a null value. Just like a container, you can put values into a variable and take them out. A variable in PHP looks something like this:

$variablename;

The dollar sign $ at the beginning of the variable tells PHP that the name following the dollar sign is a variable. The semicolon at the end of the variable name lets PHP know the variable is null. A variable can have any name you want it to as long as it starts with a letter, number, or underscore and doesn’t have any other characters in it. Let’s look at some more variables:

$red = “color”;

$number = 7;

$empty = null;

$otherempty = ;

One of the things we try to do with variable is to give them a descriptive name. A variable with a descriptive name will help you understand what’s going on in your code. However, it’s probably not a good practice to make a variable with a really really long name either. The longer your variable name is the easier it is for you to misspell the name which would generate a syntax error. All of the variables you see above are valid even though they have a different variable name.

Syntax Error – in the simplest terms, when PHP cannot understand what the code is telling it to do. PHP files go through something called an interpreter. An interpreter takes the code you’ve written and translates it into code a computer understands. When the syntax in your file is incorrect, the interpreter cannot translate the file like it needs to and this is what generates the error.

If-Else Statements

Programming languages are all about logic. They usually deal with things that happen in steps, and decision making. For instance, lets say you are going to the grocery store and you only have $15.

<?php

$money = 15; //how much money you have

if ($money > 5) //you have more then 5 dollars

{

echo "I'm buying some potato chips. ";

$money = $money - 5; //subtract the cost of chips from how much money you have

echo "I now have " . $money . " dollars.";

}

else

{

echo "I cannot afford potato chips";

}

?>

So, lets break this down. First we created a variable that we called $money to represent how much money we have. We gave the money variable the value of 15 dollars. Next, we used an if statement to check if we had more then 5 dollars. If have more then 5 dollars, then on the page we’ll see the text we have inside of the brackets {} in the if statement. Can you guess how much money it will print out on the screen? If you said 10 then you’re right! The statement $money = $money – 5; is the same thing as saying $money = 15 – 5; because the value of $money is 15.

So… what do you think happens if you changed the $money variable at the top of the pay to 3 dollars? Or, what happens if you change $money = $money – 5; to $money -= 5; Try it and find out!

Continue to Part 2 >>