Hopefully this series of articles, “Jump into PHP”, will help beginners find their way into the world of PHP, and also serve as a great place for experienced developers to find some great tips to help them out.
This article will cover setting up a PHP server and some of the very basics in PHP, and programming for that matter.
What is PHP?
PHP is a scripting language that can be embedded into HTML webpages to create dynamic web pages. PHP can also be run in a page of it’s own.
When the PHP page is then accessed the server that the page is on will interpret the code. Once the code has been interpreted the page is then displayed to the Web Browser that is viewing the page. PHP can also be accessed through a Shell on a server, or a computer.
As mentioned above PHP runs on a server. Most often a LAMPP stack. LAMPP is an Acronym for Linux, Apache, MySQL, Perl, Python/PHP. Linux is an Open Source Operating System that a Server might run on. Apache is a server, it handles the requests and responses between a client Web Browser and a Server. MySQL is a free Open Source, not for long, version of MySQL. Perl is another scripting language, as is Python the two both run through the Common Gateway Interface (CGI). Also, it uses PHP, which is what we shall be concentrating on.
I hear you ask the question, how do I get all of this onto my computer!? Well, your questions are about to be answered.
Installing PHP
Before getting started with any language you will need to know how to install it onto your computer. With PHP, however, you will need to install a stack of software to be able to use PHP.
There are a variety of different pieces of software that allow you to run PHP on your machine. My personal preference, and the example to follow, will use XAMPP. XAMPP is a piece of software that will turn you personal computer into a server that will allow you to develop in PHP.
If you have a server of your own, having XAMPP set up on your computer as you will be able to FTP your files up to the server and then run from there.
Downloading the files
Head on over to Apache Friends and download the relevant version of XAMPP for your Operating System. In my case I am downloading XAMPP for Mac OS X. There are also versions for Windows, Solaris and Linux. Take your pick!
Fortunately XAMPP have a great set of instructions fo follow when you download the file. You should be taken to them but here is a link to them:
The long and short of it is when you have installed and started up your XAMPP server and typed “localhost”, or “127.0.0.1″ into your Web Browser you should be met with a screen asking you to choose your language. When you are all set up, you should see this screen:
Congratulations, you have set up XAMPP!
Now for the fun parts!
Your First PHP File
It’s now time to make your first PHP file. It is important you know where your “htdocs” folder is. What is the “htdocs” folder? If you are familiar with a web server you will know there is a folder where all of your files go. Well, this is what the “htdocs” folder is. All of your projects will be put in there. So, lets set about finding where your “htdocs” folder is.
Mac OS X
On my Mac I found that a shortcut to my XAMPP folder had been created when I installed it. After a bit of digging around I found the “htdocs” folder was here: “/Applications/XAMPP/xamppfiles/htdocs”.
Windows
On Windows the “htdocs” file will be within the XAMPP installation as so: “xampp/htdocs”. That’s where you put your files.
Linux
On Linux you will be able to find your “htdocs” folder in this location: “/opt/lampp/htdocs/”
Solaris
As with Linux you will find your “htdocs” folder here: “/opt/xampp/htdocs/”
So, now you know where to put your stuff. Let’s get doing some PHP.
Basic PHP
Now you know where your “htdocs” folder is, you can create a new Folder. Let’s call it “myfirstphp”. When you have made this folder type “localhost/myfirstphp” into your Web Browser. You should see this page:
This screen shows you that there is something there. Open your favourite Text Editor / Web Development tool and create an empty file named “index.php”. Now when you go back to that page and refresh the page you will see an empty web page.
Congratulations! You have made your first PHP page. Now lets do some programming. We’ll start our file off with a basic “Hello World!” example.
Here is the code for it:
|
1 2 3 |
<?php
echo "Hello world!";
?> |
Every single piece of PHP we write must be written in between a set of tags, in our case we have . If our PHP wasn’t written between the tags the server wouldn’t know that it is dealing with PHP and it would just output the code as if it were HTML. We also have the statement:
|
1 |
echo "Hello world!"; |
This is just a simple piece of PHP to “echo”, as is suggested something to the screen. If you go back to your Web Browser and refresh the screen you should see this:
Yay, your PHP worked. Good job soldier.
Now it’s time to do something a little bit meatier. Have you ever heard of a loop? Not Fruit Loops, or Spaghetti Loops, but a programming Loop. Basically, a loop will repeat a task until a certain condition has been met. Then the loop will stop. Type this code into your program, underneath your “Hello World!” bit of code.
|
1 2 3 4 5 6 7 8 |
<?php
echo "Hello World!";
for($i = 0; $i < 10; $i++){
echo "Hello World!
";
}
?> |
Holy Moly, there are “Hello World!”‘s everywhere! You have just encountered the “For loop”. This cool piece of code declares a variable, “$i = 0;”, $i as 0. Then it says “For $i is less than 10 do the code in between the curly braces”, $i < 10. When the code between the curly braces has been done, add 1 to the variable $i.
We will take a brief look at another type of loop the “While Loop”. This loop is different to a “Foor Loop” as the only parameter in the loop is the condition that the loop will be done for. Let’s add this bit of code to your program:
|
1 2 3 4 5 6 7 |
<?php
$j = 0;
while($j < 10){
echo "Hello World! <br />";
$j++;
}
?> |
If you refresh your webpage now, you have a screen with 21 “Hello Worlds!”‘s on it. What does this show you? Ignoring the first “Hello World!” I have demonstrated to you one of the key concepts of programming, the Loop, I have shown that it can be achieved in multiple ways, that achieve exactly the same thing. Pretty neat, huh?
What else have you learnt? Well, not too much really, but a couple more key concepts. For example, you now know how to declare a variable in PHP, “$i = 0;”, you now also know how to increment a numeric variable “$i++”. Also, pretty neat. You also know how to echo something to the webpage the PHP is in, this is crucial for showing users information you have gathered, from a database query, for example.
Other than programming you have learnt a bit about PHP and it’s uses, you have also learnt how to set up a server.
All in all, not a bad first lesson. From teaching myself PHP and from learning on the Job, I think I have covered a couple of important points. Next lesson I plan on covering more programming than this one. Think, Arrays, Functions, If Statements, Return Statements, Switch Statements.
I hope you enjoyed the read! And please, follow me on Twitter.



