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. ...

Get PHP in a Nutshell now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.