sabato 20 agosto 2011

PHP – Introduction To PHP

php banner PHP Introduction To PHP




What makes the success of the web today is its ease of access and its simplicity. A web surfer doesn’t need to know how everything works in the background.


On the other hand, novice web masters such as you must, before anything else, know the basics of the workings of a web site. What is a server and a client ? How do you make a dynamic site ? What are PHP and MySQL ?


This first chapter is here to answer all these questions and to show you that you are capable of making a dynamic web site.


Before we start I would like to thank our friends at Le Site Du Zero for letting me adapt their lessons. icon biggrin PHP Introduction To PHP


We’ll say that there are 2 types of web sites : static and dynamic sites.

elephant PHP Introduction To PHP

Static sites : they are sites made using (X)HTML & CSS. They work very well but their content can’t be updated automatically : the owner of the site (the webmaster) must modify the source code to add anything. It’s not very useful when your site needs updating several times a day ! Static sites are therefore useful for making show case web sites, for example to present a business or company, without going any further. This type of site is becoming rarer today, because as soon as you add an element for interaction (such as a contact form), we stop talking about static sites, but talk instead of dynamic sites.


Dynamic sites : more complicated, they use other languages as well as (X)HTML and CSS, such as PHP and MySQL. The content of these sites is said to be dynamic because it can be changed without the presence of the webmaster ! Most of the sites you use nowadays, including Euro PC Solutions, are dynamic sites. The only prerequisites you need to learn to make a dynamic web site is to already know how to make sites using (X)HTML and CSS (you can read my lessons on XHTML and CSS to learn this).


The purpose of these lessons is to make you capable of making a dynamic web site by yourself, step by step.
This allows you to make more exciting things than a static site. Here are some things you’ll be able to make after these lessons :

A member area : Visitors will be able to register to your site and access reserved areasA forum : Nowadays websites often propose forums to let people help each other out or just to discussA visitor counter : You can easily count the number of visitors that have been on your site or know the number that are currently browsing !News : You can automise the writing of news and give the visitors the possibility of adding news or comments etc.A newsletter : You can regularly send an email to all your visitors with the latest news and persuade them to visit your site again

Of course, these are only examples. It’s possible to go a lot further, everything depends on your needs. You should know for example that most online games are dynamic. You can find for example games for raising animals, conquering space etc. icon wink PHP Introduction To PHP


But…let’s not get carried away. Before getting there you have lots of things to learn ! Let’s start by the basics : do you know what happens when you open a web page ? icon wink PHP Introduction To PHP


When you want to visit a web site you write its address in your web browser, whether it’s Mozilla Firefox, Internet Explorer, Opera, Safari or another. But have you ever asked yourself how the web page gets to your computer ?


You need to know that the internet is a network of computers. These can be classed into 2 categories :


Clients : They are the computers of web surfers such as yourselves. Your computer is therefore a client. Each client represents a visitor on a web site. In the following diagrams, the computer of a client will be represented by this image :

pc PHP Introduction To PHP


Servers : These are very powerful computers that store web sites and send them out to client computers. Most web browsers will never see a server in their life. However, servers are necessary for the internet to work. Servers will be represented like this :

server PHP Introduction To PHP

Most of the time, a server doesn’t even have a screen : it stays on and works without human intervention, 24h/24, 7d/7


Quick summary : your computer is called a client and the computer that stores web sites is called a server. How do they communicate ? icon surprised PHP Introduction To PHP


That’s where the difference lies between a static site and a dynamic site. Let’s see what changes.


When a site is static, the diagram is very simple. There are 2 steps :

The client asks the server for a web pageThe server answers back by sending the web pagestatic1 PHP Introduction To PHP


The communication is therefore quite simple :

Hello, I’m the client, I would like to see a web pageThere you go, here’s the page you asked for

On a static site nothing else happens. The server stores the web pages and sends them to the client when asked for without modifying them.


When a site is dynamic, there is an intermediate step : the page is generated.

The client asks the server for a web pageThe server prepares the page specially for the clientThe server sends the page that has been generateddynamic PHP Introduction To PHP


The web page is generated each time that the client asks for it. That’s precisely what makes web sites dynamic : the content of the page can be changed from one moment to another.


That’s how some sites can show for example your username on every page. Because the server generates a new page every time a visitor asks for one, it can personalise the page depending on the visitor (and show the username among other things).


When we make web sites, we use not one, but several languages. As a webmaster you must know them.

Some programs, called WYSIWYG (What You See Is What You Get), allow beginners to make static web sites without knowing any of the languages…But to make a dynamic site like we want, you must know them.


A number of languages have been invented for creating web sites. Two of them are absolutely necessary for webmasters.


XHTML : Is the language at the root of every web site. It’s a lot like HTML but imposes some stricter rules. I recommend that you use XHTML instead of HTML because it makes you take care with your code.
XHTML is easy to learn and works with tags. Here’s an example of XHTML code :


Code : HTML


Hello, I'm a paragraph of text !


CSS : Is the language that creates the design of web sites. Whereas XHTML inserts the content of a site and creates the structure, CSS sorts out the design and look. It’s with CSS that we can choose the colour of the text, the size of the menu etc. Here’s an example of CSS :


Code : CSS

.banner { text-align: center; font-weight: bold; font-size: 120%;}

These languages are used on every web site. When a server sends a web page to the client, it sends the XHTML and CSS code.

html1 PHP Introduction To PHP


The problem is that when you only know XHTML and CSS, you can only make static sites…and not dynamic sites ! To make these you need to know other languages as well as XHTML and CSS.


The question you therefore need to ask yourself is : do you know XHTML and CSS ?


If the answer is yes, that’s perfect, you can continue because we’ll need it during these lessons. If the answer is no, don’t panic. XHTML and CSS aren’t very difficult, everyone can learn them. You can learn them by reading my XHTML and CSS lessons.
You can learn these languages in a couple of weeks, or less if you have enough spare time. icon smile PHP Introduction To PHP


Whatever the site you want to create, XHTML and CSS are indispensable. However, they aren’t enough to make a dynamic site. They need to be completed with other languages.


And that is the whole purpose of these lessons : you’ll learn to use PHP and MySQL to make dynamic sites.


PHP : It’s a language that only servers can understand and that allows us to make sites dynamic. It’s PHP that generates pages like we saw on the above diagrams.
It’s the first language that we’ll discover in these lessons. It can work by itself, but is only really interesting if it is combined with MySQL. Here is some PHP code :


MySQL : It’s what we call an RDBMS (Relational Database Management System). To keep things simple, its purpose is to store data in an organised way so that you can easily find and use it. It’s with MySQL that you’ll be able to store the number of visitors on your site, the messages written on a forum etc. The language that allows us to communcicate with a database is called SQL. Here’s some code written in SQL :


Code : SQL

SELECT id, author, message, datemsg FROM visitorbook, ORDER BY datemsg DESC LIMIT 0,10PHP and MySQL are what we call freeware. Among other things this guarantees lasting quality : everybody can contribute to the development, and there’s no risk of webmasters losing interest in it all of a sudden meaning that it would be dropped, and that is very important !
PHP and MySQL are also available for free. Because of that you won’t have to give away a penny to make your site ! icon wink PHP Introduction To PHP


PHP can work by itself and is enough to make a dynamic web site, but it becomes really interesting when it’s combined with an RDBMS such as MySQL. However to simplify things, we’ll forget MySQL for the moment and concentrate on PHP. icon biggrin PHP Introduction To PHP


Clients are incapable of understanding PHP code, they only know XHTML and CSS. Only the server can read PHP.


The role of PHP is to generate XHTML code (we can also generate CSS but it’s rarer) which is then sent to the client like a static site :

php PHP Introduction To PHP


PHP is a programming language that is used on many servers to make decisions. It’s PHP that decides what XHTML will be generated and sent to the client each time.


To understand the point of that, let’s take an example. In PHP we can write : If the visitor is a member on my site and he’s called Jonathan, show ‘Hello Jonathan’ on the web page. If he’s not a member, show ‘Hello’ and give him the choice of inscription.
It’s a very basic example of a dynamic site : depending on if you’re a member or not, you won’t see the same things and you won’t have access to the same things.


XHTML and CSS don’t have any competition because they’re standards. Everybody is supposed to know them and use them on every web site.


On the other hand, with dynamic sites, PHP and MySQL are far from being the only languages available. I can’t give you a complete list of alternatives, it would be too long (and boring !). However, for your general culture, you should know some of the other big names.


To start with, even though we often combine PHP and MySQL to make powerful dynamic sites, we shouldn’t mix up the two languages. The first has different elternatives to the second language.


Among the competition of PHP we can find :

ruby PHP Introduction To PHP

ASP .NET : designed by Microsoft, it uses the .net framework which is well known by C# developers (a framework is a collection of libraries which help developers). this language can be useful if you’re used to developing in C# and don’t want to learn too much new information.Ruby On Rails : this framework works with the Ruby language and lets you create dynamic sites quickly.Django : is similar to Ruby On Rails but uses Python instead of Ruby.Java and JSP (Java Server Pages) : more often called JEE, it is particularly used in the professional environment. It demands a certain strictness. The creation of a JEE project often takes longer to design but the system is appreciated by professionals and institutions.

I can’t give the names of all the competition here, but that should give you a good idea. For information, it is also possible to use C or C++, but it is complex and not necessarily adapted (in other words I don’t really recommend it icon biggrin PHP Introduction To PHP )

Which language should I choose ? Which is the best ?


With the subject of these tutorials, you’d expect me to instantly say PHP !. But no. In fact, everything depends on your programming knowledge. If you’ve already used Java, you will quickly be able to use JSP. If you know Python, Django will be good for you.


As for PHP, it’s distinguished from the other languages because of its large community that can quickly help you on the internet if you have problems. It’s an easy to use language that is ideal for beginners and professionals : Wikipedia and Facebook are examples of popular, famous sites that use PHP.


Well, there is no best choice. I recommend the language with which you are certain to get help if you have problems. In this sense, PHP is a very good choice.


As for databases, the choice is yet again very vast.
However, whereas PHP and its alternatives are normally free, that’s not the case with most RDBMS.


Among the competition of MySQL, I recommend you know (at least by name) :

oracle PHP Introduction To PHP

Oracle : is the most famous RDBMS, the most complete and the most powerful. Unfortunately you have to pay for it (and it’s very expensive), which means it is mainly used in large companies. However there are free versions of Oracle for those who want to learn it.Microsoft SQL Server : made by Microsoft, it is often combined with ASP .NET, although it can be used with any language. You do have to pay for it but there are limited free versions.PostgreSQL : is a free RDBMS like MySQL which offers some more advanced functions. However it still has some way to go compared to Oracle.SQLite : the simplest and smallest RDBMS. It is free like MySQL but has very few functions. Its main advantage is that it’s very small.

Once again this list isn’t complete but gives you an idea of what exists.
For information, MySQL is by far the most used free RDBMS. Among the professional, paid solutions, Oracle is the most advanced and used but its use is reserved for large companies because of its price.


So really, if your budget is limited, you don’t have a lot of choice for RDBMS. MySQL is the most used because it is free, and is used by a lot of people that can help you out.


As you have seen, there is a large choice of tools for making a dynamic site. Most of them are free.


Normally you should be able to combine these tools as you wish. For example, you can use PHP with a different RDBMS to MySQL, such as Oracle or PostgreSQL. MySQL can be used with any language : Java, Python, Ruby etc.


However, the PHP + MySQL combination is the most common. It’s not by coincidence that I’ve decided to write lessons on these two tools icon wink PHP Introduction To PHP


You should now have a good idea of what PHP and MySQL allow you to do. If you remember that PHP generates personalised XHTML for each visitor, and MySQL is for storing data, you already know the main idea.


 

Nessun commento:

Posta un commento