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

Output Style

Owing to the fact that PHP generates its output dynamically, it is easy to generate messy output that is hard to read. While this is not a problem in itself, it does not look good on you and your web site, and also makes the outputted HTML source code hard to read if you have debugging to do. Help is at hand: the Tidy extension, amongst other things, can clean up and repair poorly written HTML.

Here's an example HTML document:

    <TITLE>This is bad HTML</title>

    <BODY>
    This would get rejected as XHTML for a number of reasons.
    First, the <FOO> tag doesn't exist.<BR>Second, the tags aren't the same case.
    Third, tags that don't end, like <HR>, aren't allowed.<BR>
    Tidy should fix all this for us!

As you can see, it's quite messy. Let's put it through Tidy with no particular options set:

    <?php $tidy = new tidy("lame.html");
            $tidy->cleanRepair();
            echo $tidy;
    ?>

That will output the following:

    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 3.2//EN">
    <html>
    <head>
    <title>This is bad HTML</title>
    </head>
    <body>
    This would get rejected as XHTML for a number of reasons. First,
    the tag doesn't exist.<br>
    Second, the tags aren't the same case. Third, tags that don't end,
    like
    <hr>
    , aren't allowed.<br>
    Tidy should fix all this for us!
    </body>
    </html>

Tidy has added all the right header and footer tags to make the overall content compliant, and normalized the case of the elements. Second, it has taken away the FOO tag because it is invalid. Third, it has wrapped the lines so they aren't too long. ...

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