concept PHP code in category php

appears as: PHP code, PHP code
PHP in Action: Objects, Design, Agility

This is an excerpt from Manning's book PHP in Action: Objects, Design, Agility.

What do you do? If the application is like a lot of PHP applications, the HTML markup for the client-specific layout may be intermixed freely with the PHP code. You can change the layout by changing the markup; it’s not that difficult. But having two different layouts and switching between them easily is not within the realm of possibility with this kind of structure.

In spite of all this, there are those who advocate mixing presentation information—in the form of HTML markup—into PHP code. As mentioned in chapter 11, I was surprised to find that one relatively recent PHP book actually recommends outputting all HTML code from PHP echo or print statements. Perhaps I shouldn’t have been surprised. The conclusion from looking at a number of open-source web applications is that it is common practice.

Listing 13.2. The user list PHP code after extracting the HTML section
<?php require_once 'UserFinder.php'; require_once 'HTTPPlus.php'; $finder = new UserFinder; $users = $finder->findAll(); include 'userlist_template.php'; ?>

Even if—typically on a small project—programmers are doing the design job, it’s not safe to mix presentation and program code. As mentioned in chapter 11, you never know when you might need the help of a professional web designer or when an existing application might need to be adapted to a completely different layout. This kind of thing is almost impossible without separating PHP code from HTML markup.