The PHP is an HTML element, not a PHP one.

On many Internet websites, you’ll come across article explaining you HTML and PHP. But none of them teach you that the PHP tag should be considered as an HTML element, not as a PHP one. Worse, many example you’ll find does this mistake again and again. This article is PHP centered, but apply to other technologies like JSP or most templates systems, where code is mixed with logic.

Let’s consider this example :

1
2
3
4
<p>
    This line is written in (x)HTML.<br />
    <?php echo "This line is written in PHP."; ?>
</p>

Isn’t it wonderful ?

PHP and HTML have two different indentation semantics.

When it comes to programming, indentation is mandatory. It make your code readable by make its structure visible. Some languages like python define their structure throw indentation. This isn’t the case of HTML and PHP.

HTML’s indentation goes like XML’s. Element are embedded one in another and the embedded element are one level more indented than it’s parent. PHP on its side, is a C like programming language (speaking about syntax) so indent just the same way.

You’ll often mix PHP code and HTML and so it often result in a impracticable indentation system.

Consider this simple code snippet :

1
2
3
<div class="content">
    <?php echo $some_content; ?>
</div>

It is fine and readable. But what happen when we use more complex PHP logic ? This become a mess. Actually, this piece of code is readable because the PHP part doesn’t have a construct that require indentation.

1
2
3
4
5
6
7
8
<div><?php if($pager->has_previous) { ?>
        <button class="page_left" />
    <?php } ?>
        <span><?php echo $pager->current_page; ?></span>
    <?php if($pager->has_next) { ?>
        <button class="page_right" />
    <?php } ?>
</div>

In this example, we notice that both indentations systems are mixed. You may have done it another way, but code looking like this is very common, in a way or another. First of all, the HTML in output is indented in a messed up way. Secondly this isn’t readable anymore. And it will get worse if the logic get more complex.

It doesn’t have to be that way. We just need to stop messing up HTML indentation and PHP indentation.

Get PHP’s indentation semantic separated from HTML’s one

This is pretty simple actually. First we need to acknowledge that PHP and HTML indentation doesn’t have to match each other. Second of all, we need to decide that the PHP tag is an HTML element. This element indicate that we goes form HTML to PHP world.

1
2
3
4
5
6
7
8
9
10
11
12
13
<div><?php

if($pager->has_previous) {
    ?><button class="page_left" /><?php
}

?><span><?php echo $pager->current_page; ?></span><?php

if($pager->has_next) {
    ?><button class="page_right" /><?php
}

?></div>

Notice the difference ? We don’t mix up anymore PHP and HTML’s indentation and we have some readable code. We integrate PHP tags directly within the HTML and do not follow HTML indentation in PHP code, so logic expressed in PHP is more visible. The shift is pretty easy. You just have to stop thinking you put tag around PHP code, but to stop HTML output or start it again.

This technique produce HTML that is indented incorrectly. But large chunk of HTML will still be indented the right way and still be readable. Plus, tools like Firebug allow you to explore HTML in a very nice way, even if it isn’t indented correctly.

One thought on “The PHP is an HTML element, not a PHP one.

  1. Hello, while I think content like this is useful, I don’t like the last result, here is what I usually do:

    “`

    has_previous): ?>

    current_page; ?>

    has_next): ?>

    “`

    Or if you want more indentation:

    “`

    has_previous): ?>

    current_page; ?>

    has_next): ?>

    “`

    The second example looks better (IMO) but it adds unnecessary indentation to the final HTML, but you should (maybe) compress the HTML at the end or fix the indentation with a plugin for that.

    My example works well if you want to replace the PHP tags with Smarty, Blade, Twig or other PHP template system.

    Best regards.

Leave a Reply

Your email address will not be published. Required fields are marked *