Go to the menu - Go to the contents

[Site map] You are here --- > Newbies Paradise > Tutorials > Official > Website > Reading a tutorial

Introducing PHP

Avatar
Author : M@teo21
Creation : : on 07/31/2007 03:46:03 PM
Last modification: : on 09/27/2007 11:45:57 AM
Rate and comment this tutorial
Print this tutorial
I guess your mind is made up! You want to start using PHP? So welcome!

User image
Let me introduce myself: I am M@teo21 and I will guide you through this lesson. In this first part we will discover PHP and I will explain everything to you as clear as possible. And if you follow well, you will soon find out that PHP has no secret for you anymore.

User image
Let me intoduce the elePHPant. It is the mascot of the PHP and you find it on most of some websites dealing with the topic of PHP. It is a distinguishing mark of PHP ;)

Right! But that doesn't tell us what is PHP, does it? You will get the answer in the following lines. You will also see that you will always discover PHP over and over again because its universe is so rich and wide. No one can pretend to know everything about it. There is always something to learn :)

Chapter Contents :
Contents Next Chapter

What is PHP?

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.

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 :D

Isn't it great? I guess you understand now why people are more and more interested in PHP!

Differences between HTML and 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!

User image

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.

User image

There are two steps :
  1. 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" .
  2. 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. o_O

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.
User image

Let's examine the steps one more time:
  1. 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".
  2. 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.
  3. 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: PHP
1
<?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. :p

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!

Quiz

Which animal is the mascot of PHP?

What does a PHP page contain?

What does a PHP page contain after having been generated by the server?
Which of the following can read PHP?

What is special about the page generated for a client?


We are at the end of our chapter. My aim was to give you a gentle introduction to PHP.

I tried to explain the relationship between the client and the server. It may seem a bit weird as a starting point but I am sure that this will be useful for you.
If you feel a little bit confused, don't worry: it won't be of any consequence for the rest of the lesson. The main thing was to read this chapter so that if somebody asks you about the "client/server relationship", you will not feel uneasy.:p


You wonder about what's next? Relax! There is nothing difficult. I know you like exercices: don't worry it is planned in the program. I want you first to practice quite a lot, so that I can see what you are capable of as we get along. :)
Contents Next Chapter
Author : M@teo21
Rate and comment this tutorial
Print this tutorial

Change your template | Learn more about NP | Site map | Terms of use | Rules | RSS feed | XHTML 1.0 | CSS 2.0
Powered by Simple IT SARL: Contact us

There is nothing else to read, you have to go up now !

Do you want to be published here? Contact us.

Number of Newbies connected 6 Newbies Connected | SQL requests 10 Requests | Page loading delay 0.1209s (0.1091s)