What is PHP?

What is PHP?

Understanding the basic uses of PHP – the server-side language

This article has not been written to teach you PHP, to provide a simple answer to the question “What is PHP?”. To start off on the right foot, you first need to understand a little about another language, HTML.

Understand HTML to understand PHP

HTML stands for HyperText Markup Language and is used to produce the basic building blocks of any webpage. HTML forms the building blocks behind most webpage content. In essence, HTML is an advanced version of the Markup language used in the first word-processor programs. HTML contain tags – or instructions to tell your browser to display content in a certain way. For instance, make a word <b>bold</b> or <i>italic</i>, or <div align=”center”>centre align your content</div>.

Now, let’s turn our attention back to PHP. PHP stands for ‘PHP: Hypertext Preprocessor’ (don’t ask!). The ‘Preprocessor’ in PHP is the big clue to it’s main uses – server-side processing. Basically, in a nutshell PHP is used to generate (parse) HTML code to an internet browser. It overcomes many of the limitations of HTML, which cannot be used to display up-to-date information on it’s own.

PHP can be employed to fetch information from a database and display that data in a browser, or show the currently time/date or make mathematical calculations.

The PHP constructs HTML for your browser to interpret as a web page at server-side. Here’s an example:

<?php
echo "Hello, today's date is ".date("d/m/Y").", and it shall remain that for the rest of the day!";
?>

In this example, PHP will parse the content between the quotes and also retrieve the current date via the date() function:

Hello, today's date is 11/05/2011, and it shall remain that for the rest of the day!

You can also use PHP to hold variables and make calculations. Read through the following PHP code carefully and see if you can work out how it works. PHP is generally very easy to understand if you pick through it slowly:

<?php
$income = "2500";
$expenses = "2100";
$total = $income - $expenses;
$name = "James";

if($total < $income)  { $warning_colour = "red"; }
else  { $warning_colour = "green"; }

echo "$name, you should be able save a total of <b>&pound;$total</b> each month!";
?>

This will appear in the browser as:

James, you should be able save a total of £400 each month!

The HTML will be parsed as:

James, you should be able save a total of <b>&pound;400</b> each month!

As you can see, the PHP has been used to calculate and display variable data. Such data could also be fetched from a database, or via an HTML form, or even from other files containing PHP and HTML. For instance:

<?php include("../page-parts/top.html"); ?>

What is PHP? Let me tell you all about it...

<?php include("../page-parts/bottom.html"); ?>

By using the above method, you could save hours of editing by putting much of your HTML into separate files and call it into the page when needed. The content of top.html and bottom.html is parsed directly (server-side) into your web page at the position it is included. When your browser parse it into HTML code, it’s as if the three documents are just one segment of code.

To get started

Ensure that your web server allows for you to use PHP. Most modern web servers can, but if in doubt ask your provider. Generally, files containing PHP must end with .php as apposed to .html or .htm in order for your server to parse them. When writing in PHP, your code (as with all examples in this article) must start with <? or <?php and end with ?> so that the server knows when to start and end processing PHP. Good luck!

Leave a Reply

Your email address will not be published. Required fields are marked *