I'm learning PHP - Simple Intro


Up until recently, scripting on the internet was something which very few people even attempted, let alone mastered. Recently though, more and more people have been building their own websites and scripting languages have become more important. Because of this, scripting languages are becoming easier to learn and PHP is one of the easiest and most powerful yet. I too do not want to be left behind. I have some knowledge in programming (VB, C and Java) but never dig on PHP.

What Is PHP?

PHP stands for Hypertext Preprocessor and is a server-side language. This means that the script is run on the web server, not on the user's browser. PHP is relatively new (compared to languages such as Perl (CGI) and Java) but is quickly becoming one of the most popular scripting languages on the internet.

Why PHP?

You may be wondering why I should choose PHP over other languages such as Perl or even why I should learn a scripting language at all. Learning a scripting language, or even understanding one, can open up huge new possibilities for your website. Although I can download pre-made scripts from sites like Hotscripts, these will often contain advertising for the author or will not do exactly what I want. With an understanding of a scripting language I can easily edit these scripts to do what I want, or even create my own scripts.

Using scripts on my website allows me to add many new 'interactive' features like feedback forms, guestbooks, message boards, counters and even more advanced features like portal systems, content management, advertising managers etc. With these sort of things on my website you will find that it gives a more professional image. As well as this, anyone wanting to work in the site development industry will find that it is much easier to get a job if they know a scripting language.

What Do I Need?

As mentioned earlier, PHP is a server-side scripting language. This means that the web host will need to have PHP set up on their server. If they don't, ask them to install it as it is free to download and install but no need to worry because most of the free hosting nowadays support PHP.

I download an off line web server package (also known as WAMP or LAMP) to let me write PHP offline. For Windows environment, we have easyPHP, XAMPP and for Ubuntu it is already in the repository. I already wrote a tutorial on how to install PHP on Ubuntu here.

Writing PHP

Writing PHP on my own computer is actually very simple. I don't need any special software, except for gedit text editor (like Notepad in Windows). Run this and you are ready to write your first PHP script.

Declaring PHP

PHP scripts are always enclosed in between two PHP tags. This tells your server to parse the information between them as PHP. The three different forms are as follows: (without the quotes)

"PHP Code In Here"

"PHP Code In Here"

"<"script language="php">"
"PHP Code In Here"

All of these work in exactly the same way but in this tutorial I will be using the first option (). There is no particular reason for this, though, and you can use either of the options. You must remember, though, to start and end your code with the same tag (you can't start with " for example).

My First Script

The first PHP scriptI will be writing is very basic. All it will do is print out all the information about PHP on my server. I type the following code into text editor:


As you can see this actually just one line of code. It is a standard PHP function called phpinfo which will tell the server to print out a standard table of information giving you information on the setup of the server.

One other thing you should notice in this example is that the line ends in a semicolon. This is very important. As with many other scripting and programming languages nearly all lines are ended with a semicolon and if I miss it out you will get an error.

Finishing and Testing My Script

Now I have finished my script, I save it as phpinfo.php and upload it to my server in the normal way. Now, using web browser, I go the the URL of the script. If it has worked (and if PHP is installed on the server) you should get a huge page full of the information about PHP on my server.

If the script doesn't work and a blank page displays, may be there are either mistyped code or the server does not support this function. If, instead of a page being displayed, you are prompted to download the file, PHP is not installed on your server and I should either search for a new web host or ask current host to install PHP.