Oliver Nassar

PHP and X/HTML Inline Code Standard

July 06, 2013

I took some time, sometime ago, to standardize the way I integrate my PHP and X/HTML in my files. I did it passively, and over time.

Integrating PHP and X/HTML together generally occurs when working within views of an MVC codebase/project.

I believe it's a pretty important note to make, as it can make your code exceptionally more readable, and thus maintainable. This is especailly true for new-comers to your codebase and style. Here is some sample code from a codebase, and I'll follow that up with how I would reformat it using my standard.

<?php if (($obj && $obj->property) || $other): ?>
<div id="user">
    <?php if ($obj->sub && $obj->num === 4): ?>
        <p class="paragraph<?php if ($true) echo ' margin-fix'; ?>">Sample Copy <strong><?php echo h($obj->title); ?></strong></p>
    <?php endif; ?>
    <?php if ($obj->yellow): ?>
        <div class="class-name">
            <div class="float-left">
                <?php echo linkTo(Helper::stringHelper($obj->colour, 'Yay! The colour is: ' . h($obj->colour)), formatColour($obj->colour), array('class' => 'img-wrapper')); ?>
            </div>
            <div class="left school-detail">
                <p class="pre-note">Featured in</p>
                <p><strong><?php echo linkTo('The School of ' . h($school->name), schoolUrl($school)); ?></strong></p>
            </div>
        </div>
    <?php endif; ?>
</div>
<?php endif; ?>