What is PHP?
Do you know what a website is? Don't worry I know you guys know the answer, but I promised to start from scratch, so I have to keep that promise.
Well! a website is what you are looking at right now: my website (in this case). To visit a website, you need to type in its address
http://www.newbiesparadise.com. By doing so, your browser (Firefox for instance) takes you to the site.
There are many things to do on a website: learn (what you are doing right now) play games, discuss, exchange and share information etc.
Second question: do you know what is (X)HTML? If you know what it is, great! But if you don't, you need to learn it before you go further.
XHTML is the new name of the HTML language (you have probably heard about that one). So just bear in mind that both mean the same thing i.e. the scripting language used to design a website. Most of the time in this lesson, I will use "HTML" to refer to this special language we use to create websites.
If you don't know HTML
you won't be able to learn PHP.
But fortunately for you, I have written a lesson on HTML and I advise you to
take a look here or at least to refresh your mind.
To remind you, (X)HTML is a scripting language used to design websites. By keying in a special code (made up of tags) you can put a text in bold, insert a picture etc etc.
Take a look at what a website would be with (X)HTML code:
Code: HTML 1
2
3
4
5
6
7
8
9
10
11
12
13 | <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Welcome to my website!</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
</head>
<body>
<p>
Welcome to my website !<br />
Click <a href="http://www.newbiesparadise.com/">here</a> to enter !
</p>
</body>
</html>
|
what about PHP? Well, PHP is another scripting language embedded in HTML code. Here is an example of what it may look like (it's a quick preview of what you are going to learn)
Code: PHP 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 | <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Welcome to my website !</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
</head>
<body>
<p>
Welcome on my website !<br />
<?php echo("You are visitor n°" . $nbr_visitors); ?>
<br />
Click <a href="http://www.newbiesparadise.com">here</a> to enter !
</p>
</body>
</html>
|
What is new in this code? You can notice that a new line has been added:
<?php echo("You are visitor n°" . $nbr_visitors); ?>
There is still HTML language around this line but we can find
PHP instructions embedded in. What I'm going to teach you is how to use these types of lines. It may seem a bit scary and weird with all these symbols (
$ ; ?) but you will soon get used to it, believe me

.
As you can notice, a page written in HTML language has the ".html" extension. A page with a PHP code has the ".php." extension. There are different versions of PHP, so you may come accross ".php3" or ".php5" extensions. The current version of PHP is the 5th version.
Can we find web pages written only in PHP?
Well, no! We still need HTML to create a web page. There is no way out!
To sum-up: HTML is useful up to a certain point, but has a limited scope. With PHP you are able to do more things on your website. Take a look at what you can do.
- A forum where everybody can discuss, exchange information and give help.
- A chat room to discuss on-line with other people!
- A guestbook: if visitors like your website, they can leave comments other visitors can read !
- A newsletter: it is very easy to do. You write your newsletter, you click on a button and your news is automatically sent to the members of the newsletter !
- A visible or invisible web counter. It's up to you. If you create it yourself, there is no advertisement (Don't fall in the trap of those pre-made counters that require to put an ad on your website
)
- A dynamic news system: you go on a web page, type the news and the welcome page of your website is automatically updated and accessible to visitors!
- Visitors can react to the news, make comments and offer to help etc.
PHP can even do more, but what you just read was to warm you up.
What you have to bear in mind is that
PHP enables you to create dynamic web pages, which are automatically updated even if you are away. You can be on holiday in the Bahamas and your website can still function by itself.
Another great advantage, and you will notice it, is that PHP announcs the era of the Lazy webmaster (Lazy with capital L) Once your website is functional, it updates automatically, transforms itself and you don't have to do a thing
Isn't it great? I guess you understand now why people are more and more interested in PHP!
Differences between HTML and PHP
What you are going to learn now is not too difficult. Do you know that a lot of people start using PHP and they don't even know it's PHP!
Believe me! If you make the effort to understand how it works (it only takes you 10 minutes) not only will you save time but
you will also understand what you are doing, and this is priceless
What am I going to talk about now? I will explain to you how it works when a visitor wants to visit your website. You will tell me that he just needs to write in the address! Yes but what's next? The web page displays, ok, but what really happened in the meantime?
This is really important because in HTML and PHP the process is not the same.
There is a very important thing to know here: the relationship between the client and the server. Are you scared of the word "relationship?" There is nothing weird about it!
- The client: "the client!" Well it's you! :). You are sitting comfortably in front of your computer and you want to visit a website. All website visitors are clients. Let's represent the client's computer by this picture:
- The server : there is only one server. The server is like a big computer which is permanently connected to the internet at a very high speed. This computer is located somewhere in the world, it is always turned on and nobody touches it. It functions 24/7 and it distributes your website. In other words nobody uses it to play games. Here is how it works: it contains the website on its hard drive and when a client requests a web page, it sends it to the person. To represent the server I will use the following machine (note that in general a server does not have a desktop as nobody works on it)
It's simple, isn't it?
For those who have not understood, here is an example.
Let's imagine you are in a restaurant. You are
the customer. you order something to eat. The cook is
the server: when you order a meal he brings it to you.
If another customer comes in and orders another meal, he supplies it. And he has to do so all the time.
This is the same thing on the internet. The server is a computer which sends web pages to clients who request it and the server works non-stop.
OK! now that you have understood this, let me show you the little something which makes the difference between an HTML page and a PHP page.
Before: with HTML
Let me remind you that an HTML page has an .html extension. I will not go into details but basically here is how an HTML page functions.
There are two steps :
- The client (it's you the visitor) requests a web page. He makes a request and it goes like this: "please could you send me the page holidays.html" .
- The server replies "Here is the page you requested" and sends back the page holidays.html.
Of course they talk very politely to each other!

The client wanted to visit holidays.html on a website: he requested the page to the server in charge of the site and the server sent it back to him. The visitor stares wide-eyed as the page is displayed before him.
The same process takes place everytime you visit an HTML page. But what is the difference with PHP?
Now: with PHP
There is an important step which takes place between the two processes: The PHP page is generated by the server before it is sent. Here is an illustration.
Let's examine the steps one more time:
- The client requests a PHP page. For him it makes no difference. He requests the page in the same polite way to the server: "Please could you send me the page holidays.php".
- At this point there is an important step which makes the difference in PHP. The server does not send the page to the client immediately. It generates it before. The client cannot read a PHP page (only the server can do so). The client is only able to read HTML pages.
What the server does is very simple. It transforms the PHP page into an HTML page so that the client can read it.
- When the page is generated it only contains HTML code. The server can send it to the client: " here is the page you requested".
Let me tell you some more on the second step, where the page is generated. It is very important that you understand what is going on during this process.
What does "generating a PHP page" mean?
Earlier in this chapter, we worked on the following segment of PHP code.
Code: PHP1 | <?php echo("You are visitor n°" . $nbr_visitors); ?>
|
Clients' computers cannot read PHP code: they can only read HTML code. The task of the server is to transform the PHP code into an HTML.
What is the importance of the PHP code then?
The PHP code contains
instructions. It requests the server to perform actions like giving the time, the number of connected people on the site etc. In a word, the PHP code gives commands to the server. It was not possible to do so with HTML. With PHP it is possible and you will notice many changes.
Do not forget that a PHP page contains HTML code.
The server does not change this HTML code. When it meets a PHP code, it reads it, executes the order and transforms it into HTML code.
At the end of the process, the generated page contains only HTML code which the client is able to read.
What is particular in this process is the fact that the generated page is intended to only one client. When another client makes a request, the server generates a new HTML page.
This means
each generated page can be unique. This is another great advantage compared to HTML where the page was always the same because the server would only send the files. With PHP the server works for the client and sends him a personalized page.
Note that generating a page can take some time (a few milliseconds depending on the size of the page)
The server needs to be more powerful than a normal HTML server to be able to generate PHP pages... If your website is popular, you won't have only one client requesting a web page, but 28 clients at the same time!