Quag's PHP Basics Tutorial

Since several people have asked me how to code in PHP (and I already had a guide up for HTML), I thought I'd add a basic tutorial for PHP. Writing PHP code requires at least a basic knowledge of HTML, but it can be written in any text editor (such as Notepad - though many people find Crimson Editor useful for line numbers). To test the code, however, you'll need something that can parse the code on your computer (or the ability to upload to a website with PHP access).

Starting and Ending
PHP files start with:
And end with:
(While you can drop in and out of PHP within the same HTML Document, it's easier on the server, or so I've been told, if you simply embed the HTML within the PHP.)

One of the most important things to do in any programming language is add comments. It helps you keep track of your thoughts while coding something, and it helps others who come after you figure out what you were thinking when you coded something. There are two main methods of commenting in PHP.
The first is for single lines only:
// Quagsire needs Water Absorb to render Water-type attacks useless.
The second is for blocks of text (I usually use these for my opening description in a program, which tells when I coded it, what it's *supposed* to do, and why I needed it):
Ode To Quagsire
Quagsire so squishy; Quagsire so blue.
In battle my team often turns to you.
With earthquake so strong and yawn so sneaky,
You face any foe, no matter how cheaky!

To output text, you use the Echo command:
echo "Quagsire!";
You can also use it to insert variables into the output, like this:
echo "Quagsire pwns ".$anyPokemon."!";
HTML tags can also be embedded in the output:
echo "<B><FONT color=#7FD8FF>Quaggy Quagsire!</FONT></B>";

Variables hold data of many different types. Here's an example of intializing a couple variables:
$bestPokemon = "";
$myPokemon = "Quagsire";
$bestPokemon = $myPokemon;

(There's a host of variable handling functions in PHP, far too many for me to cover them all here. For a much more complete reference, see www.PHP.net)
The above variables are all of the string data type. Strings can be concatinated, like this:
$QuaggyQuaggy = $myPokemon.$bestPokemon;
The other simple data types deal with numbers. PHP can do quite a few arithmatic functions (though I'm only going to cover the basics here):
$PokedexNumber = (5*(6+7))*3;
$PokemonWorld = 649 % $PokedexNumber;

(The top one equals 195, and the bottom one, the modulus, divides the first by the second and gives only the remainder, so the answer is 65.)

Flow Control: If-Else and relatives
Flow Control is the ability to change the flow of the code based on a certain condition. We'll start with an if-else statement:
if ($yourPokemon == "Quagsire") {
_echo "Your Pokemon is so cool!";
} else {
_echo "Meh, my Pokemon is cooler."

(This basically says that if the variable $yourPokemon is equal to the string "Quagsire", display the text "Your Pokemon is so cool!". Otherwise, display the text "Meh, my Pokemon is cooler.". Do *NOT* forget the }. You will get a parse error when you try to run the code. That error will say something about "Unexpected T_End" on the last line of your program code, and you will feel like bashing your head against a wall. Yes, I speak from experience. If you value your sanity, just don't forget the { }s.)

A more simplistic version of the if-else statement uses a ternary operator (and eliminates the pesky { } s):
$Pokemon = ($level < '20') ? "Wooper" : "Quagsire";
(This says that if $level is less than 20, the $Pokemon equals "Wooper". Otherwise, it equals "Quagsire".)
Here are a couple more examples of comparisons:
$warning = ($level >= '36') ? "Quagsire probably knows Earthquake!" : "Quagsire may not know Earthquake.";
(If $level is greater than or equal to "36", $warning equals "Quagsire probably knows Earthquake!". Otherwise, it equals "Quagsire may not know Earthquake.".)
$newMove = ($level != '48') ? "Quagsire won't learn Mist/Haze right now." : "Quagsire learns Mist/Haze!";

(If $level is not equal to "48", $newMove equals "Quagsire won't learn Mist/Haze right now.". Otherwise, it equals "Quagsire learns Mist/Haze!".)

Sometimes simple comparisons aren't enough. For this reason, you can use logical operators to combine comparisons.
if (($yourPokemon == "Wooper") || ($yourPokemon == "Quagsire")) {
_echo "My Quagsire hands you a Rare Candy for your Pokemon.";

(If $yourPokemon equals "Wooper" or "Quagsire", my Quagsire gives you a Rare Candy.)
if (($yourPokemon == "Quagsire") && ($level <= '50')) { echo "Care for a friendly battle?"; }
(If $yourPokemon equals "Quagsire" and $level is less than or equal to '50', I request a friendly battle with you.)

Next, we'll move to Switch statements, which can be useful to replace a big if-else statement.
switch ($PokedexNumber) {
_case "194":
__$thePokemon = "Wooper";
_case "195":
__$thePokemon = "Quagsire";
__echo "Your Pokemon is not of my clan. Please try again when you have a quaggier Pokemon.";

(If $PokedexNumber equals "194", $thePokemon equals "Wooper". Otherwise, if $PokedexNumber equals "195", $thePokemon equals "Quagsire". Otherwise, tell the trainer their Pokemon is not part of Clan Quaggy. And, in case you are wondering, yes, "quaggier" is a real word.)

Flow Control: Loops

Before we move on to Loops, I want to add a word of warning. It is possible to set up a loop that never ends. This is a VERY bad thing. Do not do it. Computers don't like endless loops.

The first loop we'll cover is the while loop:
$level = "1";
while ($level <= "53") {
_switch ($level) {
__case "5":
___$newMove = "Mud Sport";
__case "9":
___$newMove = "Mud Shot";
__case "15":
___$newMove = "Slam";
__case "19":
___$newMove = "Mud Bomb";
__case "24":
___$newMove = "Amnesia";
__case "31":
___$newMove = "Yawn";
__case "36":
___$newMove = "Earthquake";
__case "41":
___$newMove = "Rain Dance";
__case "48":
___$newMove = "Mist/Haze";
__case "53":
___$newMove = "Muddy Water";
___$newMove = "";

(While $level is less than or equal to 53, use the Switch statement to determine $newMove. If $level isn't 5, 9, 15, 19, 24, 31, 36, 41, 48, or 53, then there is no new move. After the comparison, $level is incremented. ++ adds 1 to the number, whatever it might be. This is neccessary to prevent endless loops. We'll revisit this example when we get to Arrays.) ... To Be Contined ...