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
The following figures illustrate how both client side and server side
Let’s look at this step by step:
- A user clicks on a link .His web browser sends the request for the
web page e.g. http://localhost/test.php.
- 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.
- 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.
- The interpreter places the result into an HTML page.
- 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 from the 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:
<p> This is HTML inside a paragraph tag and is ignored by the php interpreter.</p>
echo “hello this is php code interpreted by the php interpreter”;
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 has executed the code and returned pure HTML to the browser.
To run php scripts you will need a host that support php (almost all do) or you can set up your own test environment on your PC using the Xampp server.
If you would like to learn more about PHP I would suggest you get yourself a book.
References and other Resources:
- PHP for Beginners
- Really good beginners tutorial on object oriented php
- Video- An Introduction to PHP
- Video Learn PHP in 15 Minutes
- Starting HTML
- Cascading Style Sheets for Beginners