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.
Another bright spot in the learning of PHP is that many of the programming constructs you will see demonstrated here you already know from other languages, be it C, C++, JavaScript, etc. Looping constructs such as if...else, if....else...if, for loops, while loops, do...while loops and switch statements will all be found in PHP. Operators such as arithmetic, conditional, and logical operators look very much like you will have seen in other programming languages. So that part of the learning curve curve will move quickly. Of the usual constructs and operator the thing which will be somewhat different is the syntax of handling arrays although the semantics and purpose of arrays is identical to that found in other languages.
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
Some Basic Knowledge of HTML, CSS, and JavaScript
Some of the things you might want to consider before embarking on these tutorial is whether you have an adequate background to proceed. PHP can interact with HTML, CSS, and JavaScript. In fact PHP files can contain elements of these other languages as well as text. If you do not have a background in these or are just a little rusty and need some review you might consider look at some of my tutorials which I keep adding to from time to time. For HTML the series starts with "A Web Tutorial Series: Creating Your Own Presence on the World Wide Web - Introducing HTML 5". For CSS, it begins at " A Tutorial Series CSS for Beginners: Cascading Style Sheets - An Introduction" and for JavaScript, A Beginner JavaScript Tutorial Series: An Introduction to JavaScript - Identifying Basic Requirements.
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
<?php
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.