Skip to Main Content
PHP in a Nutshell
book

PHP in a Nutshell

by Paul Hudson
October 2005
Intermediate to advanced content levelIntermediate to advanced
372 pages
11h 35m
English
O'Reilly Media, Inc.
Content preview from PHP in a Nutshell

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.

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 PHP—don'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.

Interpreting Versus Compiling

Behind the scenes, PHP compiles your script down to a series of instructions (called ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

PHP Cookbook

PHP Cookbook

Eric A. Mann
Programming PHP

Programming PHP

Rasmus Lerdorf, Kevin Tatroe
Learning PHP

Learning PHP

David Sklar

Publisher Resources

ISBN: 0596100671Errata Page