build-website-header
spacer-image
 

Starting PHP

This is the first part of a 5 part articles series to help you understand and start using php.

An Introduction to PHP

PHP was originally designed to create dynamic web pages and to be interpreted by the web server making it independent of the web client (browser) being used.

 It is mainly used in this context as a server side scripting language.

How Server Side Scripting Works

However, you can use server scripting to create pages that can reach any browser with just pure HTML as the script is processed on the server.
The following figures illustrate how both client side and server side scripting works.
 

server side scripting




Let's look at this step by step:

  1.  A user clicks on a link .His web browser sends the request for the web page e.g. http://localhost/test.php.
  2.  The web server gets the request for test.php. It knows that .php files are handled by the PHP Interpreter (by the file extension .php) so it tells PHP to deal with it.
  3.  Test.php is a PHP script that contains commands. The commands are passed to the interpreter. The choice of interpreter will depend on what type of script we are dealing with.
  4. The interpreter places the result into an HTML page.
  5.  The HTML page is returned to the client.
     

PHP Files and File Extensions

PHP scripts are commonly mixed with HTML code in the same document. So that the server knows to pass the file through the PHP script interpreter the files are given the php file extension.

Because php script is mixed with HTML the interpreter needs some way of identifying what is php script, and what is HTML.

The php script is identified to the interpreter by placing it inside the script tags:

<?php  or <?  -------opening tag

?>  ---------closing tag

So if we take a look at the following file:

<HTML>
<Head>
<Head>
<Body>
<p> This is HTML inside a paragraph tag and is ignored by the php interpreter.</p>
<?php
echo "hello this is php code interpreted by the php interpreter";
?>

</Body>
</HTML>
 

I've highlighted the php in yellow. If you placed this file on as server as test.php this is what you would see in a browser.

If you then look at the source code. This is what you see:

Notice that you don't see the php tags or code. This is because the interpreter as executed the code and returned pure HTML to the browser.

Starting PHP- Part 2