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.
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 scripting is mixed with HTML the interpreter needs some way of identifying what is PHP 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 has executed the code and returned pure HTML to the browser.
WordPress and PHP
WordPress is an application written in PHP and although you don’t need to be able to write PHP code to create a WordPress site having a basic understanding will come in useful.
Learning PHP
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
- Starting JavaScript
- Cascading Style Sheets for Beginners