21 February, 2009
1

Learning PHP Series: Part 1 - What PHP isn't

In order to teach a few of my colleagues how to program in PHP, I’ve decided to begin a series of blog articles on the topic. Ultimately, I’d like to bring anybody following the series all the way up through such advanced topics as object oriented programming, design patterns, and unit testing, although getting to that point is going to take us a while.

A basic knowledge of HTML and web development in general is assumed in this series. If you are not familiar with HTML, I suggest you at least take a look at it before starting this series. There are numerous resources on the internet and probably hundreds of books about HTML and beginning web development out there. If you don’t know HTML, you should still be able to follow along, but really, learn HTML first. It will be better for everybody.

What is PHP?

Every introduction to PHP ever written has included the history of the language and how it came to be so popular. While some may find that information interesting, it really has little to do with learning PHP, so let’s just skip it. If you’re really interested, check out this brief history of PHP.

PHP is a general purpose scripting language, but it’s especially well suited to web development. It is an interpreted language (as opposed to compiled languages such as C or Visual Basic). To explain the difference between compiled and interpreted languages is beyond the scope of this series, so I’ll just say that interpreted languages sacrifice some performance for ease-of-use and a lot shorter development cycle. In other words, they may not be as fast as compiled languages, but they’re much easier to learn, and they take far less time to create an application the average person can use.

PHP is actually written in C, which to many new programmers seems somewhat of an impossibility. How can a programming language be written in another programming language? And why? Wouldn’t it be easier just to write web applications in C? A good analogy is that of a model plane. Although it’s possible to build an entire model plane from scratch ( C ), it would take a lot less time to build it from a model plane kit (PHP).

What PHP isn’t

For a lot of PHP beginners, there is a blurry line between PHP and the web technologies it makes use of (such as HTML and HTTP). This is especially true for those who are new to programming all together. Let’s take a minute to outline just what PHP’s not.

PHP is not HTML

This is a distinction that can be hard to make for newbie programmers. The problem, I think, is that all over the web you will find tutorials saying that PHP can be embedded into HTML pages. I think this is where the confusion begins. PHP cannot be embedded into HTML pages. It is in fact the other way around. HTML can be embedded into PHP pages. If PHP is installed on your server and you create a page with the .php extension (even if it’s a blank page) it is still handled by PHP (which simply outputs it to the browser).

Are you confused yet? Let me explain. Other programming languages such as python and ruby are geared towards a much broader spectrum of programming (much of it completely unrelated to web sites). PHP, on the other hand, was built specifically for the web. It’s main goal has always been to get information from the server to your browser. As such, PHP is in output mode by default. Until you explicitly tell it you want to go into php mode, it will just output whatever you put in the file.

<html>
    <head>
        <title>Hello World</title>
    </head>
    <body>
        <p>All this HTML is being output to the browser.</p>
        <p><?php echo strtoupper("This is actual PHP code"); ?></p>
    </body>
</html>

In the previous example, everything inside of < php and ? > is executed as PHP code. Everything else is simply output to the browser as if it were a regular HTML file.

Although the majority of PHP scripts/applications are intended to output HTML, it can output all kinds of formats such as xml, json, csv, and even PDF.

PHP is not HTTP

HTTP is a request/response protocol which is used to fetch web pages (or other resources) from a web server. While PHP makes heavy use of HTTP, it is by no means the only way to use the language. PHP can be very useful for writing command-line scripts, and even desktop applications. Because HTTP is the most common way to use the language, PHP naturally has many features and capabilities that lend themselves extraordinarily well to the protocol, so it can be easy to blur the line between the two.

PHP is not interactive

I’m often asked questions about how to make things such as pop-up windows, real-time clocks, draggable elements, etc. with PHP. The answer is that it simply cannot do these types of things. The reason for that is that PHP runs on the server. By the time a page has loaded in the web browser, PHP has long since finished processing. For these types of tasks, you must use a client-side solution such as Javascript or Adobe Flash, which are run completely within your browser.

PHP is not always the best tool for the job

Many PHP developers will try to have you believe that PHP can do anything other than the interactive stuff we just talked about. PHP is a very powerful language and it is capable of just about anything you’ll ever need to do in a web application, but often just because you can do something doesn’t mean you should. PHP has its weaknesses. For many tasks, PHP is just not the best choice for the job. For instance, PHP is capable of running scheduled tasks, but if available, cron is often a much better solution. Sometimes, the most important thing you need to know about a language, is when not to use it.

Summary

That pretty much sums up what PHP is and is not. In the next part in the series, I’ll show you how to install PHP on your system, whether you use Windows, Mac, or Linux. This will allow you to write and test your own PHP scripts completely free of charge and without the need to upload them to a web server.

Posted in PHP, Tutorials at 5:36a.m.

Comments

One Response to “ Learning PHP Series: Part 1 - What PHP isn't”

  1. Globally Recognized Avatar for None

    Hey Luke, stubbled across this post while on DN.

    You did a really good job in this article, you did excellent keeping things simple and written in plain English.

    I`m always interested in reading how others see PHP and programming in general.

    Cheers,
    Alex