Tuesday 6 September 2011

A PHP Tutorial

PHP is an excellent server-side technology for dynamic webpage generation. Within a span of few years, it has gained immense popularity among developers. This excellent 5-Part tutorial written by Luigi Arlotta explains the basics of PHP programming. His style is so simple that even absolute beginners would have no trouble following this tutorial.

This tutorial is targeted at those users who may have never programmed using any language before. Also programmers who have experience in other languages can quickly browse through this series and get their PHP code running within minutes...



Introduction

PHP stands for "PHP: Hypertext Preprocessor". PHP is a scripting language through which you can generate web pages dynamically. PHP code is directly inserted in HTML documents through specific TAGs declaring the code presence and then executed when a client demands the page. PHP is a server-side language, that's to say that PHP code is directly executed by the server, while the client receives processed results as an HTML document. This way of working is different from that of other scripting languages as JavaScript, whose code is first loaded onto the client machine and then executed by the client (the browser).

A few points to note about PHP programming -

1. All compatibility problems existing between different browsers are completely solved. The Client's browser, receives a normal HTML page after the execution of a PHP code on the server, and so it is always able to display it correctly since it deals with only HTML. This does not happen with scripting languages interpreted by the client's browser. In this case the client downloads the script code and tries to process it on the local machine. This procedure works correctly only if the client is equipped with the right software (generally called plugins or built-in support in the browser).
2. The server side code processing sees to it that the script code is never visible to the clients. That prevents "thefts" of source code.
3. The server side code execution requires that your webserver has been well configured. It must be able to recognize HTML documents containing PHP code. In order to make this, it is necessary to install a PHP engine and to edit some lines in the webserver's configuration file.
4. Server side code processing needs resources (CPU time) for generating the dynamic pages. A high number of client requests could overload the server. But generally today's servers such as Apache are made stable enough to handle a relatively large number of clients.
To make the webserver differentiate between HTML documents containing PHP code and normal HTML pages, .php, .php4 or .phtml extensions are used in place of the .html . These extensions can change according to the webserver configuration. We shall stick to the standard .php extension.
Assume that a client requests the following page (example1.php). The source code for the file is shown below
<HTML>
<BODY>
<?
echo ("<H1>This is an example</H1>");
?>
</BODY>
</HTML>

This page will be recognized, thanks to the extension from which it is characterized and it will be processed as a HTML document containing PHP code by the server. The code is interpreted before the output is sent to the client. The webserver passes this page through the PHP engine which processes it and executes the PHP instructions in the code. It then substitutes the result of the execution in place of the original PHP code. Once processed by the PHP engine, the webserver then transmits this dynamically generated page to the client. The client receives the following HTML document

<HTML>
<BODY>
<H1>This is an example</H1>
</BODY>
</HTML>


As you see, in the document sent by the web server to the client there is no sign of PHP code. The code has been interpreted/processed and replaced with HTML lines. The client will never be able to deduce what code generated the particular HTML lines.

PHP instructions are placed inside special TAGs <? and ?>

These tags allow the PHP engine to distinguish the PHP syntax from the rest of the document. It is possible to use different TAGs editing the engine configuration file php.ini . We shall stick to the most common ones - the ones shown above.

Necessary Setup to Run PHP on your machine
To start writing and testing your PHP scripts you need a webserver and a PHP engine. The PHP engine must be combined with the webserver so that the webserver can process the PHP code in your pages. As far as webservers are concerned, on the Linux platform - Apache rules. It is probably the most used webserver at the moment. You can download Apache from www.apache.org. The PHP engine (release 3 or 4) can here be downloaded from www.php.net

You could learn how to configure Apache to run PHP with the help of the manual that you would be provided or you could search for articles on Tips For Linux itself.
First steps

My first PHP script consists of the classic program that displays the famous " HELLO WORLD ". Copy the code shown below, paste and save it in a file with the name helloworld.php in your webserver standard directory (or in the webserver's php files directory).
<HTML>
<BODY>
<?
echo ("<H1>Hello World!</H1>");
?>
</BODY>
</HTML>

Then start your web browser and type the following in the address bar -

http://127.0.0.1/helloworld.php

or

http://127.0.0.1/~username/helloworld.php


Note : In case you have not yet configured Apache Server you could read Article No. 29 which explains Apache Server's configuration. Once you Apache Server is configured, you could continue with this article.

If it works correctly you can proceed and read the rest of this tutorial, otherwise you would have to do some more tweaking and get PHP configured on your machine. If you can't solve the problem, before abandoning, try to have a look to the software documentation. Even try posting your problem on discussion forums on the Web. You will definitely find a solution.




Variables

A variable is a block of memory, accessible through a name chosen by the software developer, in which a value is stored. This value, is usually given a default value at the beginning of the application, and you can change that value during the execution of the program. PHP requires variable names to begin with the dollar ' $ ' character.

Variable names can be composed of uppercase and lowercase letters, digits and underscores. But it is not possible to include spaces or other special or reserved characters to define the names of a variable. Remember that PHP is a case-sensitive language. This means it distinguishes between uppercase and lowercase variable names. For instance, if we write the following script. The code below shows the case sensitive aspect of PHP.

<HTML>
<HEAD>
</HEAD>
<BODY>
<?
$VAR1 = 5;
echo ($var1);
?>
</BODY>
</HTML>

We'll get an error message because the PHP interpreter does not find $var1 variable (The variable defined initially was $VAR1).

A script involving variable declarations and displaying the values of those variables is shown below. Read it carefully because it contains some important points that we will discuss later.
<HTML>
<BODY>
<?
$website = "http://www.bitafterbit.com";
echo ("<BR>Surf to: $website");
echo ('<BR>Surf to: $website');
?>
</BODY>
</HTML>

The program defines and prints the variable $website. Observe that in the echo() function, whose purpose is to write a string on the screen, two different kinds of inverted commas are used - they are the single and double ones.

Important
: In this tutorial we will refer to the double inverted commas (") as only inverted commas and we will refer to single inverted commas (') as only quotes. The main difference between these two types of syntax is that PHP interprets what is enclosed in the inverted commas, while everything appearing between quotes is considered a constant value and it is not interpreted.

The example given above produces the following output


Surf to: http://www.bitafterbit.com
Surf to: $website

The text $website is in fact interpreted and replaced with the value of the corresponding variable only in the first echo() statement. This because in the first statement we have used inverted commas to enclose the text to print. The result of this first instruction is


Surf to: http://www.bitafterbit.com
As explained previously, in the second echo() statement, where quotes have been used in place of inverted commas, the enclosed text is not interpreted, because it is considered to be a constant. The output of the second echo() instruction therefore is


Surf to: $website
It is important to take notice of this important characteristic of PHP. It is different from other programming languages where all that appears enclosed between inverted commas is considered a constant value (a string). We will talk about strings in detail in later articles when we'll discuss PHP's datatypes.

No comments:

Post a Comment