A Server Side Web Tutorial Series # 1 - An Introduction to PHP, An Open Source Server-side Scripting Language
PHP Utilized in Hundreds of Millions of Web Sites
What is PHP? What is it Good For?
PHP is yet another programming language but before you let a groan out let's talk about it a bit. I believe you will see the necessity of knowing something about if you anticipate doing some web development that PHP occupies a unique niche.
Now, to the original caption heading questions: "What is PHP?" and "What is it good for?"
PHP as far as acronyms is concerned has a a somewhat checkered history. Some claim that it stands for "Personal Home Pages" while others claim that the letters came first and then the name was chosen. If that's true then PHP developers came up with "hypertext preprocessor". In any case, PHP is a powerful, server side programming language installed and used in millions of web sites. We'll look at its features in the next section.
Features of PHP
Although PHP can also be used as a general purpose programming language it is though of most as a web scripting language which is server-side based. Some of the major features of PHP include:
- the ability to dynamically generate web page content
- it can manipulate data in a data base. The popular option which is used in practice is to install PHP in conjunction with MySQL to create, modify, delete data in a MySQL repository.
- PHP can provide data protection through encryption
- PHP can interact with web "cookies"
- it can act as an access control mechanchism
- it can harvest form data entered by users and manipulate that data.
Advantages of Using PHP
The first advantage of using PHP is that it is open source and free. Free is always a good word when one talks about technology implementation. Free is only good if the product is good. Some of the other advantages of PHP are:
- it is a cross-platform product. It runs on Windows, Linux, Mac OS X, UNIX
- it is compatible with most web servers today (We will focus on its use with Apache.)
- besides MySQL is can support other databases
- its relatively easy to learn.
Administration of MySQL Databases is Often Handled by PHP
Some Prerequisites and Useful Links Before Starting
An XAMPP Installation is Prefered for Windows
For the purpose of this tutorial we will be using an XAMPP installation as our webserver on our local host. For those following on LINUX or Mac OS based systems, appropriate packages are available for your operating systems. The reason for this is the simplicity of the installation process, the ease of administering it, and the fact that it also contains several useful products which are installed with it: MySQL, a useful database to experiment and possibly later implement with, FileZilla, a file transfer program, Mercury, a mail server product, and Tomcat, which is the open systems implementation for Java Servlet and Java Server Pages. Perl, PHP, and PHPMyAdmin are also included in the easily installed zip file. For more information, you might want to consult my XAMPP tutorials at "A Tutorial Series Testing Your Web Devlopment: Part I. Setting Up Your Own Local Web Server Using XAMPP". I will be adding to this series discussing some of the very few problems I encountered with it use.
The XAMPP Control Console
A Sanity Check: Saying "Hi, There!" in a PHP Script
At this point, we assume that you have successfully installed either XAMPP (or one of its variants for the other operating systems supported.) We are now ready to begin. A good text processing program can help spot errors and generally has features which help catch syntax errors. In the Windows environment there are many such programs available. Some are at a reasonable cost and they do have some "extras". As far as free products are concerned I generally use Notepad++ since wherever I happen to be consulting, it is either present of I can download it on the fly.
So let's start. The simple program will be just an echo of the statement, enclosed in beginning and ending php tags, as follows
echo 'Hi There!';
In order to run this program you will need to save it in the htdocs subdirectory of the xampp directory with the file extension of php. The following snapshot shows two things: 1) how I coded the example, and 2) the file name extension must be php.
Another test you may want to perform is:
- Go to your XAMPP console. If you do not have it started as yet. Click on your shortcut (if created or from a command line enter the path to the control executable (e.g. c:/xampp/xampp-control.exe). When that screen displays you can check out various thing about PHP by clicking on phpinfo() in the left-hand navigation bar or by looking at the sample programs.
The PHP phpinfo() Method Accessed From XAMPP
Wrap Up and What's Next
In this tutorial we discussed the features and advantages of using the PHP product in server-side web development. We indicated that rather than installing php singly as a product, a better approach would be to install XAMPP for Windows (or its equivalent for Mac OS X, LINUX, UNIX, etc.
We looked at several feature of an XAMPP installation: phpinfo(). We created the very simple PHP program including only the PHP tags and a simple echo statement and launched it from a Chrome browser window after stating the three requirements: 1) the filetype must be php; 2) it be saved in the htdocs subdirectory (to keep it simple at this point in time), and 3) referred to in the browser as localhost/filename.
In the next tutorial, we move rather quickly through a number of items: comments, variables, and operators. With a reasonable understanding of their common use and some, even limited programming background we should be able to accomplish this in one session.