Section 1.2. Advantages of PHP


1.2. Advantages of PHP

If you ask a group of PHP programmers why they use PHP, you will hear a range of answers"it's fast," "it's easy to use," and more. This section briefly summarizes the main reasons for using PHP as opposed to a competing language.

1.2.1. The HTML Relationship

When used to output text, PHP is embedded inside the text in code islands, in contrast to languages like Perl, where text 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 and then in PHPdon't worry about what the code means for now:

     #!/usr/bin/perl     print <<"EOHTML"     <html>     <body>     <p>Welcome, $Name</p>     </body>     </html>     EOHTML

And now in PHP:

     <html>     <body>     <p>Welcome, <?php print $Name; ?></p>     </body>     </html>

The PHP version is only three lines shorter but easier to read, because it doesn't have the extra complexity around it. Some modules for Perl (particularly CGI.pm) help, but PHP continues to have a lead in terms of readability. If you wanted to, you could write your PHP script like the Perl script: switch to PHP mode and print everything out from there.

Apart from legibility, another advantage to having most of the page in HTML is that it makes it possible to use integrated development environments (IDEs), whereas products like Dreamweaver and FrontPage muddle up Perl's print statements.

1.2.2. Interpreting Versus Compiling

Behind the scenes, PHP compiles your script down to a series of instructions (called opcodes), and these instructions are then executed one by one until the script terminates. This is different from conventional compiled languages such as C++ (but unlike Java), which compile the code into an executable run time and then run that executable whenever the code is encountered again. This constant recompilation may seem a waste of processor time, but it helps because you no longer need worry about recompiling your scripts when you make any changes. On the flip side, many scripts take longer to compile than they do to execute; fortunately, that is nullified by the use of PHP code caches.

One major advantage to having interpreted code is that all memory used by the script is managed by PHP, and the Zend Engine automatically cleans up allocated memory 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 isn't to say you should be lazy and make PHP do all the workthere are functions available for you to specifically clean up your memory, and you should use them if you have very tight memory requirements.

1.2.3. Output Control

In general use, PHP is embedded inside HTML in code islands that start with <?php and end with ?>, but 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, PHP code can look almost identical to the Perl code by printing the HTML from inside our PHP code:

     <?php             print "<html>\n";             print "<body>\n";             print "<p>Welcome, $Name</p>\n";             print "</body>\n";             print "</html>\n";     ?>

The print( ) function outputs the text enclosed in quotation marks to the client. "\n" means "start new line in the output " and it serves as a "pretty printer"something that makes the output look more attractive.

PHP also has powerful output buffering that further increases your control over the output flow. An output buffer can be thought of as a place where you can queue up content for outputting. Once you start a buffer, any output is automatically put into that buffer and not seen unless the buffer is closed and flushed.

The advantage to this output buffering is twofold. First, it allows you to clean the buffer if you decide that the content it holds is no longer needed. When a buffer is cleaned, all its stored output is deleted as if it were never there, and the output for that buffer is started from scratch.

Second, output buffering allows you to break the traditional ordering of web pagesthat 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.

1.2.4. Performance

PHP is one of the fastest scripting languages around, rivalling both Perl and ASP. However, the developers continue to target performance as a key area for improvement, and in PHP 5.1 (still under development at the time of this writing), many areas have seen significant optimization.

When combined with a code cache, PHP's performance usually at least doubles, although many scripts show much larger increases.



PHP in a Nutshell
Ubuntu Unleashed
ISBN: 596100671
EAN: 2147483647
Year: 2003
Pages: 249

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net