Czech version
logolink

< Back to the list of lessons

Introduction to PHP

PHPContent of the lesson:

  • Dynamic Web
  • Principle of PHP
  • Exercises

Dynamic Web

In the previous lessons aimed to the creation of web pages using HTML and CSS you saw how to create a static website. A simple text file was created (usually using the .htm or .html extension) and a CSS file to define the appearance of the website was added. If you open such a file in your web browser it reads it line after line and displays it. This way is fully static - you see exactly the same what is written inside the HTML file. No information can be changed automatically. All these pages are being called static (we do not consider technologies like Javascript or dynamic styles of CSS).

Internet if full of www pages nowadays and some of these pages display information which are requested by a person or which are being processed later. Consider a few examples:

  • Internet e-mail service - this service surely consists of dynamically generated pages because every user can log in using his username and password which have to be verified. After successful login users see their emails and can manipulate with them. Imagine a freemail server which hosts 5 million mailboxes (www.seznam.cz hosted this number of mailboxes in 2006). It would not be possible to create all pages by hand. Such a website cannot use static approach anymore. There has to be a program (algorithm) which can evaluate all actions and then create the page which is shown to a user inside his browser.
  • Electronic shop - some kind of a registration form, for example to any e-mail service (www.seznam.cz, www.centrum.cz etc.) or to a electronic shop (www.eproton.cz). Such a webpage has to be able to read information from user, check them and then display them again or ask user to change something. In every step you get a HTML page which has to be displayed inside a browser.
  • Internet calculator (http://web2.0calc.com/).
  • Search engines like Google or www.seznam.cz.

There are several technologies which allow you to get dynamic www pages:

You should remember the basic technologies to create World Wide Web.

The connection of these three technologies does not allow much nowadays. It allows you to create electronic documents in HTML which are linked to each other using hypertext (a system of links). To have a web which is interactive and to have the possibility to generate HTML files on the fly (to create pages which change throughout time), you should realize that every HTML page is a file saved on a hard drive of WWW server and it has its unique URL. You can simply create an executable file using any URL. This idea formed CGI (Common Gateway Interface) and first forms appeared (http://cs.wikipedia.org/wiki/Common_Gateway_Interface).

A user can input information and send it to a server which runs a CGI-script (program written using a programming language). This script processes the data and returns a HTML page which is displayed to user. To write CGI scripts you can use languages like PERL, C++, JAVA etc.

At the beginning these technologies were expanded (due to slow transfer speeds) using JAVA or JavaScript which are run on the client side and reduce the size of transferred data. Using the JavaScript is limited by browsers because each browser uses another implementation of this language. Java also demands quite high performance but has one great advantage - you do not have to worry about the type of operating system which runs on the client computer.

Principle of PHP

PHP works on the principle which was described in the previous part. PHP is a programming language to create scripts which is mostly used for creating dynamic internet pages. PHP scripts are usually processed at the server site and the result is transferred to a client. A user requests a page from a server, this server processes the request (runs the PHP script and gets a result - HTML code in most cases) and transfers the result to the user. The final HTML file is not a static text file but it is the result of the PHP script (result of a program).

Simple example of a PHP script test.php:

Script test.php
 <?php
  print("Hello, this text was written by PHP!");
 ?>

If you run the previous script (you place it on www server and request it), you will see a HTML page which will contain only the string "Ahoj, já jsem text vypsaný programem v jazyku PHP!". You can see the similarity to Pascal which uses a similar command to write to console - writeln('Ahoj, já jsem text vypsaný programem v jazyku PHP!');. The mark <?php means the beginning of a PHP script (a mark for the server to consider the following text as a program). The mark ?> means the end of a PHP script. This simple script contains only one print command, which gets one parameter and writes it to a browser.

Individual Task

Find a virtual web server in the Internet which can be used in your computer to be able to run PHP scripts.

WampServer

You might have found WampServer which can create a virtual server on your computer to launch PHP scripts and test your web applications. Wampserver can be downloaded for free in the bottom part of this page (you can choose 32-bit or 64-bit version). You do not have to change anything while installing, the program automatically places its working directory to C:\Wamp\. You can find a sub-directory www where you will place all your PHP scripts. It is a good habit to create additional sub-directories to better orientate in your files.

When you launch WampServer, you will see a small icon in tray - this icon is red at first and then changes to orange and to green if everything was done successfully. In case that it remains red or orange you should solve a possible error. You have to close Skype while using WampServer because these programs use the same ports. Another conflict service is IIS (Internet Information Service) which is a part of Professional and higher versions of Windows - you can uninstall or deactivate this service. In case you want to use WampServer and IIS together, you should open the configuration file C:\Wamp\bin\apache\Apache2.2.17\conf\httpd.conf and change the port after command Listen to 8080.

Localhost u WampServeru

You will see this screen when you open your browser and enter the address localhost (in case you changed your port you have to use localhost:8080). You can see the headline Your Projects in the bottom part, all your PHP files will be listed here and can be run as files on a server.

WampServer also allows you to use an internal database which can be controlled by the link phpmyadmin in the bottom part.

Additional Texts

Links

Questions

  1. Can you describe the difference between a static and a dynamic page? What are the advantages?
  2. What do you imagine under the term PHP?
  3. How can PHP be inserted into a page?
  4. What other technologies can be used to create a dynamic webpage?
  5. Can you run PHP on every computer?
webdesign, xhtml, css, php - Mgr. Michal Mikláš