PHP programming

This section is by Marfisa.
Don't be intimidated by the tutorial. If you don't understand, you can still use PHP goodies. We promise.


Syntax means the rules for writing data and commands in a programming language and it can vary greatly from language to language. For the purposes of this tutorial, I'll assume the reader has no other programming experience and I won't compare PHP to any other other programming language.

I'll start with a definition of basics. None of this stuff will make much sense by itself, but will be useful to understand other tutorials.


PHP is invisible to the end user. This means that whenever you bring up a PHP file in your browser and view the source, you'll never see any PHP code, just normal text and HTML code. So how do you show the results of your PHP code on your page? Use the echo command.

Try making a PHP file with this line:

<? echo "Hello world!"; ?>

You can even insert HTML code through PHP.

<? echo "<font color=red>"; ?>

helpful hints

Since PHP already uses things like single and double quotes as part of the code itself (the quotes, for example, mark the beginning and the end of a string of text), when you want to write those symbols, you have to but a backslash in front of them. A backslash indicates that the next character is to be treated as plain text, not code.

for example...
\" - a double quote, NOT the the beginning or end of text
\' - a single quote, NOT the the beginning or end of text
\$ - a dollar sign, NOT part of a variable name (see below for the section on variables)
\\ - a backslash, NOT an indicator that the next character is to be treated as text

There are even special codes do things like add line breaks for readability.

\n - line break
\t - tab
\r - carriage return (for output to a printer)

So normally...

echo "<font color=red>";
echo "<font color=red>";
echo "<font color=red>";

...would produce <font color=red><font color=red><font color=red>.


echo "<font color=red>\n";
echo "<font color=red>\n";
echo "<font color=red>\n";

...will produce...

<font color=red>
<font color=red>
<font color=red>

Both double quotes and single quotes are valid in PHP. Usually, you can treat them interchangeably, but in some cases, using single quotes will cause the code to work incorrectly. Learn to use double quotes throughout your code. Although it's tempting to use single quotes and double quotes in the same line in order to see it more easily, it may not always have the desired result.

This is what I mean:

<? echo '<font color=\"red\">\n'; ?>

This will output <font color="red">\n, instead of adding a line break.


Variables are used to keep track of information. You can name and assign values to them. Variables always have a dollar sign in front and the name can start with a letter or an underscore, but not a number, though the name can have a number in it. For ease of reading script, variables are usually in lowercase, but they don't have to be.

Examples of variables are $hello, $_wow, and $l33t.
Naturally, it's better if the names make sense, like $randomcard or $randomtoken.

You cannot use the same name twice!
If you want two names that are similar, try something like $variable1 and $variable2.

Values are assigned to variables with an equal sign and can be numbers, words, sentences, or whatever type of information you are dealing with. Words and sentences should be enclosed in either single or double quotes. If numbers are enclosed in quotes (either single or double), they will still be treated as numbers and can be added or multiplied, etc. As always, end each assignment with a semi-colon.

Some examples...
$text="Hello world!";

Don't put commas in numbers. For example, 1,000,000 should be written as 1000000.


An array is a type of variable that holds more than one piece of information and keeps it in sequence. Think of it as one of those little rectangular weekly pillboxes, each slot holds the pills for that day. An array is defined like this:


Since the default sequence starts with zero, tylenol is in slot 0, aspirin is slot 1, and midol is in slot 2. And it will just continue on for however many items are in the array.

To call up a specific value in an array, enclose the slot in brackets. If you want to call up the aspirin:

useful tips for arrays

A function called "count" will tell you how many items are in an array.

A function called "sort" will sort an array in ascending order.

A function called "rsort" will sort an array in descending order.


Loops are a great way to deal with arrays. A basic type of loop is a "while" statement and is means "while this condition is true, do something." When the condition is no longer true, the loop will stop.

Here is an example that will print the numbers 1 to 10.


while ($i <= 10)
echo $i;

First I assign a value to my counter, $i. You can name your counter anything, but I've used $i which stands for iteration, which is the number of times a loop runs. In this case, it starts at one. I only want the loop to run until it reaches ten. Each time the loop runs, I want it to print out my counter with echo $i, then increase it by one.

$i++ is a shortcut way of writing $i=$i + 1.
If you were subtracting, you could use the shortcut $i-- instead of writing $i=$i - 1.

Note that the condition for the loop is in parenthesis and what it does is enclosed in curly brackets.

symbols used for common comparisons:
> greater than
>= greater than or equal to
< less than
<= less than or equal to
== equal
!= not equal

Now let's use a loop to show us everything that's in an array.

1. Define the array that we used above, $pillbox.
2. Set up the counter, $i=0;.
3. Count how many things are in the array with $limit=count($pillbox);.
4. Set up the condition while ($i <= $limit), so the loop stops when the limit is reached.
5. Print out the value of the array with echo $pillbox[$i] \n;.
6. Add one to the limit with $i++;.
7. Finally, the loop stops when the limit is reached.



while ($i <= $limit)
echo $pillbox[$i] \n;

This will output:

This seems complex and kind of pointless, but it's important to understanding the rest of the PHP tutorials.

conditional statements

Otherwise known as if statements. This is how passwords gates work. You take variables and compare their values. If a condition is true, then do something.


if ($test == 4)
echo 'Test is equal to four';

There's also an "else" that you can add if you want to do something when the condition is false.


if ($test == 4)
echo 'Test is equal to four';
echo 'Test is not equal to four';

what now?

And that concludes our lesson on the basics of PHP programming. You can use these few simple things to do an amazing amount of stuff. Why not check out some of these goodies?

- Password gates use variables and if statements.
- Marfisa's randomizer is simple once you understand arrays and echo statements.
- It's easy to make an automatic Slots game with arrays and if statements.

useful hints!
- When you have nested loops or ifs, it's nice to indent the inner ones so you can see them easier.
- To add comments to your code, at the end of a line put // and then type your comment.

note from Rahenna

Don't worry if you didn't completely understand the tutorial. The individual goodies pages have examples that are completely explained and that can be copied, pasted, and easily customized. (Cuz to be honest, I don't understand all the theory myself, but I can still do it!)