php lesson 3

Your first php program

Ok lets have some fun, fire up your wampserver. Once you've started that up, you should have a little half moon shaped icon in the bottom right hand of your screen looking like this:

When you mouse over it, it says "server Offline" this is good, we're using it as test laboratory, nothing more. Putting it online without propper precautions could compromise your computer's security against hackers and the likes, so we'll leave it offline.

Now if you left click that icon, you should get a menu looking like this:

For now, all we're going to concern ourselve's with from that menu is the folder labeled as "www directory". Clicking that option will open up that folder(which you can also get to by going via your c drive C:\wamp\www).

This folder should be empty apart from a single file called "index.php file", leave that one alone. Right click in the white space of the open folder and choose the option new folder:

Create a folder and name it "my_first porgram". Now left click the wampserver icon again and this time choose the Localhost option. You should see a wampserver page with a bunch of info on it, look for the heading "Your Projects", under that heading you should see the folder you just created.

Now if you've followed my html tutorials, you will remember our discussion about a root directory(review it here if you need to). For the sake of these tutorials, the folder "my_first porgram" that you just created is going to act as your root directory and you will be able to see the results of your work via that localhost page.

At the moment there's nothing in that folder, so lets get to it and make a php file to put in there, open up your editor. Most programming tutorials, php or otherwise do a stupid first program which does nothing more than print out "hello world" on the screen. While this does give you an example a very common function used in programming it's not very exciting, and there's only so many tutorials you can follow that show you the words "hello world" before you lose the plot and go psycho.

So lets do something a bit more fun, in your editor, type the code below, don't worry too much if looks a little confusing and daunting at first, we'll break it up later and go through each bit step by step:

<?php
$middle='was';
$moods=array('happy','sad','excited','angry','bored');
$whats=array('dog','clown','monkey','mouse');
$first_mood=($moods[array_rand($moods)]);
$what=($whats[array_rand($whats)]);
$second_mood=($moods[array_rand($moods)]);
echo "The ";
echo $first_mood." ";
echo $what." ";
echo $middle." ";
echo $second_mood."!";
?>

Now save this file in your C:\wamp\www\my_first_program folder as "happy_sad_what.php". Now go to that localhost page in wamp, click on your folder and click on your happy_sad_what.php file. You should get something like this.

Don't be alarmed if it isn't exaclty the same as mine. Try refreshing the page a few times and see what happens.

Ok when you've finished saying WTF!!! and calling me a stark raving lunatic, ask yourself if that wasn't more fun than this.


When you're finished messing around with the emotions of those poor monkeys, clowns, dogs and mice, move on to the next lesson here where we'll break down that code and make some sense out of what's going on there.


Lesson added Friday 1st May 2009