What is PHP?

A Brief Introduction to PHP Scripting

© Mark Alexander Bain

Sep 29, 2008
What is PHP?, Mark Alexander Bain
PHP forms the backbone of many web sites - this article has a look at what PHP is and how to start using it.

PHP is an acronym often repeated in web site design circles and a recent survey (Nexen.net, March 2007) found that, of the 10.1 million servers polled, 33.93% were running PHP; but what is PHP? This article gives a brief introduction to PHP and shows how it can be used.

What is PHP?

PHP is a programming language, although to be more precise, it's a scripting language - that's to say that it uses an interpreter whenever a script is run rather than having a fully compiled program on the system. PHP can be run from the command line, but the most usual place for PHP is within a web page where it can add dynamic content.

One important thing to remember about PHP is that it is a server script not a client script - any code runs on the server and returns an HTML output to the client (in this case the web browser) - this is unlike Javascript or VBScript where the code is downloaded on to the user's pc and then run.

This means, of course, that a PHP script can only be run on a web server that already has PHP installed on it.

What Does the Acronym PHP Mean?

When PHP was created by Rasmus Lerdolf in 1994 he called it the "Personal Home Page" tool, and by 1997 it was known as "Professional Home Pages", but it is now more commonly as "PHP: Hypertext Preprocessor" (although, according to Rasmus, it may also mean "Pretty HTML Programs" or "Programmers Hate Perl").

A Simple PHP Script

<HTML>
<HEAD><TITLE>A Simple PHP Script </TITLE></HEAD>
<BODY>
<?PHP
phpinfo();
?>
</BODY></HTML>

From this code it is possible to see the fundamental rules of a PHP script:

  • PHP code can be placed in amongst HTML code but must be included within the <PHP...?> tags (although some servers will allow the short form (<?...?>) and ASP tags (<%...%>))
  • each line of PHP code must end in a semicolon

The code also shows that functions (both built-in and user defined) may be called from within a PHP script by typing the function name; in this case the phpinfo function is being called - this displays the configuration details of the server.

Running a PHP Script

A PHP script can be run in one of two ways:

  • from the command line
  • through a web browser (for instance Firefox or Microsoft Internet Explorer)

If the script above is saved into a file called phpinfo.php then it can be run from command line by calling it via the PHP interpreter:

php phpinfo.php

However, if the PHP script is to be run via a a web browser then it must first be saved in a directory that is accessible from the web server (for example on Debian Linux it would be somewhere like /var/www/htdocs). Once the file is in the correct location then it can be called directly from the web browser:

http://192.168.1.3/phpinfo.php

PHP and Variables

One of the great things about PHP is the ease with which it handles variables - especially variables sent to it from web pages. For example, a typical way of sending variables to a PHP script would be something like:

http://192.168.1.3/logon.php?fname=Mark&sname=Bain

The information passed to the PHP script can be processed very simply (obviously within a file named logon.php on the web server):

<?PHP
$fname = $_REQUEST['fname'];
$sname = $_REQUEST['sname'];
echo "Name is " . $fname . " " . $sname;
?>

and it's worth noting that $_REQUEST works with both the GET and POST methods.

PHP Functions and Libraries

Any complex or repetitive tasks can be broken down into PHP functions:

<?PHP
function show_name ($fname, $sname) {
echo "Name is " . $fname . " " . $sname;
}
show_name ($_REQUEST['fname'], $_REQUEST['sname']);
?>

and code can be reused in more that one web page by placing the functions in another text file (known as a library). So, in this case, if show_name was to be placed in the server_functions.php library (remembering to start the file with <?PHP and ending it with ?>) then the logon.php code would become:

<?PHP
include("server_functions.php");
show_name ($_REQUEST['fname'], $_REQUEST['sname']);
?>

Conclusion

PHP:

  • is a server scripting language used in many web pages
  • requires the PHP interpreter to be installed on the server
  • must have the code encapsulated within the <?PHP and ?> tags
  • loads variables passed to it into the $_REQUEST array
  • can use functions and can reuse those functions when they're in a library

and, by the way, that's all for free.

Further Reading

Suite101.com PHP Programming

PHP and Forms


The copyright of the article What is PHP? in Computer Programming Languages is owned by Mark Alexander Bain. Permission to republish What is PHP? in print or online must be granted by the author in writing.


What is PHP?, Mark Alexander Bain
Simple PHP Code, Mark Alexander Bain
Simple code, but impressive result, Mark Alexander Bain
   


Post this Article to facebook Add this Article to del.icio.us! Digg this Article furl this Article Add this Article to Reddit Add this Article to Technorati Add this Article to Newsvine Add this Article to Windows Live Add this Article to Yahoo Add this Article to StumbleUpon Add this Article to BlinkLists Add this Article to Spurl Add this Article to Google Add this Article to Ask Add this Article to Squidoo

Comments
Oct 24, 2008 8:07 AM
Guest :
no comment..good job
Nov 25, 2008 2:03 PM
Guest :
Thanks Mark. That was helpful.
Apr 20, 2009 12:12 PM
Guest :
thanks.It is helpful to get the minimum idea about PHP
3 Comments