Write a comment on this article

Introduction to PHP

Because HTML is a markup language (it describes where and how content is placed on a website) and not a programming language, it doesn't contain operations and functions to interact with a user. For this reason, a programming language like PHP is required. Unlike JavaScript, which is executed in the browser, PHP runs on a webserver, far away from the client.

PHP: Hypertext Preprocessor (better known as PHP) is probably the most famous programming language for the world wide web. Its structure is similar to other programming languages like C or Java, there are variables, statements, functions, arrays and comments. But it also contains some unique features which make programming with PHP so easy and comfortable.

Variables are written with a leading $ and are declared without a datatype. The datatype will be assigned automatically at runtime by PHP's engine. Here is a simple example:

$my_number = 10;
$my_decimal_number = 2.5;
$my_string = "Hello World!";
 

PHP provides many functions like string replacement, string output, array generation, database connection, and many more. Additional libraries may be included to extend PHP's function set. Object-oriented programming is possible with PHP too.

To understand how the world of PHP works, here's a small scenario:

  1. A browser sends a request to a server over the internet.
  2. The server receives the request, searches the according document and recognizes it as a PHP-file
  3. The server now parses the document (all PHP commands are executed), this may contain calculations, file-handling or database-access, anything that is possible with PHP.
  4. The result, an HTML-file, is returned to the browser.
  5. The browser receives the file and shows it to the user. Neither the user nor the browser know what happened between the request and the answer from the server.

PHP commands are embedded in between HTML tags. Whereever PHP code should be executed, the special tags <?php (opening tag) and ?> (closing tag) are used. Example:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title><?php echo "Title of this site"; ?></title>
  </head>
  <body><?php
      $a = 5;
      $b = 3;
      $c = $a + $b;

      echo $c;
  ?></body>
</html>
 

This code would result in the following output:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Title of this site</title>
  </head>
  <body>8</body>
</html>
 

And the user would see the following result:

Figure 1: Result of a simple PHP example

Now you are ready to make your first page with PHP. You can also try some of the tutorials or download free scripts.

To make websites more interesting and more interactive, databases play a big role. In combination with PHP the free database MySQL might be the most commonly used. PHP provides several functions to interact with MySQL and other databases.

Of course it's possible to install PHP locally. You will need a webserver (like Apache HTTP server) and of course PHP. Additionally your can install MySQL to work with a database. There are many installation guides online which may help you.

For more information about PHP see the resources below. Happy PHPing!

Tags

PHP, introduction

Comments (2)

Leave a comment

If you have a question or a problem and you're looking for a solution, please use the forum. Comments might not be answered.

Your name (required, maximum 255 characters) ( remember)
Your message (required, maximum 5000 characters)
 
Insert these letters into the textfield below: PODS (required, case-sensitive)
Drag the slider to the right (required)