Skip to main content

Learning PHP: The What's and the Why's

Anyone who has designed web sites for any length of time knows the limitations of html and other client side languages like CSS and JavaScript. While these languages remain at the core of web development, their primary function is to control how text and graphics are presented. Because they lack the ability to manipulate information on demand, or communicate with web servers, the result is a static web page.
The ability to create dynamic pages opens doors. Suppose you wanted to create a survey to collect information from visitors to your web site. You could easily create a form with HTML that included all your questions, and have the results emailed to you. But to convert the raw information into a meaningful format, you'd have to manually compile it, and then organize it to gauge the results - a process which could take hours.
Fortunately for you, using scripting, there are easier ways to collect and evaluate the results of the survey, or any form. Rather than sending the information through email, a script grabs it and stores it in a database on the server. A second script communicates with the database, gathers and groups the information, and prints it out when you visit the page. The script could also produce bar graphs, percentages and totals, or present the information any other way you could conceive; automatically, and on demand.
So what language do you use when you want to analyze survey results? Or offer personalized information to your visitors, or display an archive of journal entries without managing copious HTML files?
In recent years, more and more developers have been turning to a relative new-comer to the scripting world for the answer: PHP.

What is PHP?

PHP, which stands for PHP Hypertext Preprocessor, is a server-side embedded scripting language. In non-technical terms: a PHP processor is run on the server (Windows, or a flavor of UNIX). When a page is requested that contains PHP, the processor translates and executes all the commands in the page, and then outputs the result to the browser as regular HTML. Because this translation occurs on the server, a page written with PHP is viewable with any browser, on any operation system.
Like most other scripting languages, PHP can be embedded directly into HTML. PHP code is separated from HTML by Start and End entities. When a document is parsed, the PHP processor only interprets the demarked areas, and outputs the results in the same position.
Ironically, PHP also includes the ability to almost completely separate code from HTML. For larger, collaborative projects this method is ideal because it allows designers to work on the layout of the page without interfering with the code aspects.

A Little History

PHP was introduced in 1994 as a loose collection of freeware scripts based on Perl and dubbed "Personal Home Page" Tools. The author, Rasmus Lerdorf, received surprising interest in the package from the professional and developer communities. By 1995, a mailing list had been established where other developers could provide feedback, bug-fixes and code ideas on the scripts.
Encouraged to expand the original package with additional features, Lerdorf released PHP-FI (or PHP2) in 1995. This version included the ability take the information submitted by forms on the web and convert it to usable variables. What was so important about this function was that it enabled user input to be captured and acted upon, allowing more complex and interactive web applications to be developed.
It was around this time that PHP changed from being a one-man project to one with a group of 7 core developers. Together, they refined the syntax of the language, added additional functions and methods, and the ability for other programmers to extend the capabilities of the language by plugging in modules.
With the release of version 3 in 1998, PHP had finally grown into its own. Like C and Perl, PHP is a structured programming language with variables, functions and classes. Its similarity to these languages encouraged experienced programmers to migrate to PHP and its ease of use rapidly won new users.
By version 3, PHP also included database support for a wide variety of platforms including mysql, mSQL, ODBC, Oracle and Sybase. It also was able to work with images, files, FTP, XML, and a host of other technologies.
The latest release of PHP is version 4. Rebuilt with a more powerful core processor, the new PHP engine (the Zend parsing engine) offers significant speed improvements over previous versions. PHP4 also includes sessions support (an easier way of working with cookies), and numerous smaller additions and improvements.
To date, PHP is still free and a frontrunner in the open source movement. However, unlike many open source projects, it's becoming more and more mainstream as an increasing number of businesses and organizations are making the switch to it.
For professional developers, one of the most exciting things has been the release of the Zend Encoder, which allows PHP source code to be encrypted. The Encoder carries a hefty price tag, but it holds the promise of more built in value for those selling custom scripts.

Why PHP?

It's no secret that there are alternatives to PHP: ASP, Cold Fusion, and Perl, to name just a few. While each of these languages has differences in syntax and structure, when it comes down to it, they can all produce the same results.
So, why would you choose PHP over other options?
- Simplicity. For people new to programming, this is frequently the strongest appeal. Even those with little or no programming experience can quickly get up to speed and begin creating full-fledged applications. Because it was specifically designed for creating web applications, PHP has a host of built-in functions to handle common needs.
- PHP is Open Source. Because PHP's source code is freely available, a community of developers is always working to improve, add to, and find bugs in the language. Open Source means you never need to rely on the manufacturer to release the next version if something doesn't work or pay for expensive upgrades.
- Stability, and compatibility. Currently, PHP runs stable on a range of operating systems including most flavors of UNIX, Windows and Macs and integrates well with most popular servers including IIS and apache.
PHP is also endowed with other goodies, like native support for many popular databases, an extensible architecture, and a processor that not only uses fewer resources on the server than many of its competitors, but also displays pages in record time.

Finally…

This article is the first in a series, covering everything from the basics of PHP to advanced topics and including information on related subjects such as databases and XML, as they apply to PHP.
Generally, PHP is a very easy language to learn. If you have worked with another structured programming language like C, or Perl, some of PHP will seem very familiar to you, and you'll find the language very easy to pick up. But keep in mind that previous experience in programming is not a pre-requisite for learning PHP or for understanding the concepts presented in this series. The only thing that is required is a desire to make the leap to server-side programming, and a thorough understanding of HTML.
About every week or so, we'll be taking a look at a new concept or theory of the language. The emphasis will be on providing the tools needed to start programming real, usable applications as quickly as possible. Cumulative, real-world examples will provide a taste of how to structure your own scripts, as well as present solutions to situations you will often encounter in your own work.
Whether you began reading because you needed to complete a specific project, or because you were just curious, by the end of this series you can expect to know what's involved in building dynamic data-drive sites. You will also have a virtual toolbox of code snippets and script design tricks you can pull out when needed.
Most importantly, you will have the knowledge to successfully develop your own projects from start to finish.

Find the original article here

Comments

Popular posts from this blog

Survey says: PHP passes Microsoft Active Server Pages

By JT Smith on June 11, 2002 (8:00:00 AM) With a faltering economy forcing companies to cut spending whenever possible, less expensive and freely available Open Source software solutions may be gaining in popularity. Those wanting proof can look no further to PHP taking the top server-side scripting spot in a recent Internet host survey. In April 2002, Netcraft's monthly Web server survey revealed that 24 percent, or around 9 million of the 37 million sites it surveyed, were using Hypertext Preprocessor (PHP) for a server side scripting language. For the first time, an Open Source scripting solution had passed Microsoft's proprietary Active Server Pages scripting to claim the top spot on the Netcraft survey. For both the April and May Netcraft surveys, PHP and ASP were almost too close to call, with Microsoft's product offering coming in just a hair under 24 percent of all hosts running a server-side script

PHP Code Review Guidelines

General  The code works  The code is easy to understand  Follows coding conventions  Names are simple and if possible short  Names are spelt correctly  Names contain units where applicable  There are no usages of magic numbers  No hard coded constants that could possibly change in the future  All variables are in the smallest scope possible  There is no commented out code  There is no dead code (inaccessible at Runtime)  No code that can be replaced with library functions  Variables are not accidentally used with null values  Variables are immutable where possible  Code is not repeated or duplicated  There is an else block for every if clause even if it is empty  No complex/long boolean expressions  No negatively named boolean variables  No empty blocks of code  Ideal data structures are used  Constructors do not accept null/none values  Catch clauses are fine grained and catch specific exceptions  Exceptions are not eaten if caught, unless explicitly documente

Security: Password Hashing

In this article I'm going to cover password hashing, a subject which is often poorly understood by newer developers. Recently I've been asked to look at several web applications which all had the same security issue - user profiles stored in a database with plain text passwords. Password hashing is a way of encrypting a password before it's stored so that if your database gets into the wrong hands, the damage is limited. Hashing is nothing new - it's been in use in Unix system password files since long before my time, and quite probably in other systems long before that. In this article I'll explain what a hash is, why you want to use them instead of storing real passwords in your applications, and give you some examples of how to implement password hashing in PHP and MySQL. Foreword As you read on you'll see that I advocate the use of a hashing algorithm called Secure Hashing Algorithm 1 (or SHA-1). Since I wrote this article, a team of researcher