Practical PHP Programming

Peter Kitson

ISBN : -

Order a printed copy of this book from Amazon --UNAVAILABLE--

Cover Design - Practical PHP Programming

For your free electronic copy of this book please verify the numbers below. 

(We need to do this to make sure you're a person and not a malicious script)



Sample Chapter From Practical PHP Programming
     Copyright © Paul Hudson

2.2     Advantages of PHP

I could cop out here and say that the advantages of PHP are too many to list in such a small book as this, but it seems that some people need convincing of PHP's inherent greatness, and I am very happy to oblige.

If you are not new to PHP, I still recommend you at least glance over the contents of this chapter. I find that programming in PHP is very often like playing my guitar. I can be sitting in my study strumming idly on my 12-string like I've done thousands of times before, only this time I play a different chord sequence, or strum the strings in a different manner than normal, and I think "Hey, that's cool... how come I never found that before?"

At various points in my PHP programming past, I have stumbled across functions or functionality in pretty much the same way, and responded with pretty much the same response! If you're the same, then definitely read on - it doesn't hurt to see what's on offer.

2.2.1     The HTML relationship

When used to output HTML content, PHP is embedded inside HTML in code islands, as opposed to in Perl, where HTML code is embedded inside the Perl script. The most common way to open and close PHP code blocks is by <?php and ?>. Here is an example of a simple page, shown in Perl first then in PHP - don't worry about what the code means for now:

print "<HTML> ";
print "<BODY> ";
print "<P>Welcome, </P> ";
print "</BODY> ";
print "</HTML> ";

And now in PHP:

<P>Welcome, <?php print ; ?></P>

As you can see, the PHP version is only a line shorter, but infinitely much easier to read because the majority of the page is just HTML. Some modules for Perl (particularly help, but PHP continues to have a big lead in terms of readability. If you really wanted to, you could write your PHP script like the Perl script: switch to PHP mode and print everything out from there. However, it tends to get messy - as you can see!

Apart from editing legibility, another advantage to having most of the page in straight HTML is that it makes editing with commercial IDEs possible, whereas products like Dreamweaver and FrontPage trash Perl's print statements.

One key advantage to using PHP as opposed to some other solutions is that PHP code is all executed at the server, with the client only receiving the results of the script. What this means is that users never see your PHP source code because they are never sent it - they only see what you want them to see.

2.2.2     Interpreting vs. Compiling

PHP uses a blend of interpretation and compilation in order to provide the best mix of performance and flexibility to programmers.

Behind the scenes, PHP compiles your script down to a series of instructions (called opcodes) whenever it is accessed. These instructions are then executed one by one until the script terminates. This is different from conventional compiled languages such as C++ where the code is compiled down to native executable code then that executable is run from then on. Instead, PHP re-compiles your script each time it is requested.

This constant recompilation may seem a waste of processor time, but it is actually not all that bad because you no longer need to worry about hand recompiling your scripts when you make any changes. On the flip side, many scripts take longer to compile than they do to execute!

Furthermore, it provides very quick feedback during development. If you have an error somewhere in your file, PHP will refuse to compile the page until you have fixed the problem, and you are able to step through execution of your code line by line until you find the problem.

The speed hit of regular compilation is nullified entirely by the use of PHP accelerators.

One major advantage to having interpreted code is that all memory used by the script is managed by PHP, and the language automatically cleans up after every script has finished. This means that you do not need to worry about closing database links, freeing memory assigned to images, and so on, because PHP will do it for you. That is not to say you should be lazy and make PHP do all the work - good programmers clean up themselves, and let PHP work as backup in case something is missed.

2.2.3     Output Control

PHP offers a great deal of flexibility as to how you want to output your content. In general use, PHP is embedded inside HTML in code islands started with <?php and ended with ?>.

You can reverse this by writing your whole script as one big PHP code island and printing HTML as necessary. Going back to the example shown previously, we can make our PHP code look almost identical to the Perl code by printing the HTML from inside our PHP code:

print "<HTML> ";
"<BODY> ";
"<P>Welcome, </P> ";
"</BODY> ";
"</HTML> ";

"Print" is a simple function that outputs a chunk of text, enclosed in quotation marks, to the client. " " means "start new line in the source code", and it serves to lay the source code out nicely. For the longest time, a debate raged on messageboards and mailing lists as to whether it was faster to drop out of "PHP mode" to output large amounts of HTML, or whether it was just as fast to stay in PHP mode. The truth is that it is horses for courses - you will find little or no speed difference either way.

PHP purists like to point out that print is technically not a function, and, technically, they are correct. This is why print doesn't require brackets around the data you pass to it. Other language constructs that masquerade as functions (and are referred to as functions herein for the sake of sanity) include echo, include, require, and exit.

PHP also has great output buffering features that further increase your control over the flow of output. An output buffer can be thought of as a storage hole where you can queue up content for outputting. Once you start a buffer, any output you create is automatically put into that buffer, and is not seen unless the buffer is closed and flushed - that is, sent to your visitor.

The advantage to this output queuing is two-fold. First, it allows you to clean the buffer if you decide not to output the current output queue in the buffer. When a buffer is cleaned, all the output stored in there is deleted as if it were never there, and output for that buffer is started from scratch.

Secondly, output buffering allows you to break the traditional ordering of web pages - that of headers first and content later. Owing to the fact that you queue up all your output, you can send content first, then headers, then more content, then finally flush the buffer. PHP internally rearranges the buffer so that headers come before content.

Output buffering is covered in much more depth later.