[FX.php List] PHP Question
Steve Hannah
shannah at sfu.ca
Thu Jul 20 17:08:59 MDT 2006
On 20-Jul-06, at 3:51 PM, Kevin Futter wrote:
> On 21/7/06 3:25 AM, "Bob Patin" <bob at patin.com> wrote:
>
>> I don't know why I never thought to use ECHO rather than to move
>> in and out of PHP code... !
>>
>> This is much more elegant; I'll definitely store this away for
>> future use...
>>
>>
> That’s an interesting comment Bob. I tend to go the other way – I
> try as hard as I can to separate data processing logic from both
> structural and presentational layers.
I agree with you on this approach, as you are describing the MVC
(Model-View-Controller) pattern. Unfortunately PHP, on its own,
doesn't cater all that well to MVC. It works well for page layouts
where you only have to drop a view variables into a largely HTML page:
e.g. <title><?=$mytitle?></title>
...
But as soon as you add control logic with "if" statements and loops
you end up with a mess like:
<? if ($condition){ ?>
<p>Some information:</p>
<ul>
<? foreach ($myvars as $key=>$value){ ?>
<li><?=$key?> is <?= $value?></li>
<? } ?>
</ul>
<? } ?>
You can see how it would be easy to lose one of the closing braces in
the midst of a bunch of html code. The above example becomes:
if ( $condition){
echo "<p>Some information</p>
<ul>";
foreach ($myvars as $key=>$value){
echo "<li>$key is $value</li>";
}
echo "</ul>";
}
A little bit more sightly and much easier to keep track of the
opening and closing braces. Of course if we start putting too much
html in there then it could become difficult to keep track of the
html tags and we have the same problem in the other direction.
The best way to do MVC in PHP is to use some sort of templating
engine like smarty so that this is done in a way that is easy to
follow and debug in both HTML and PHP terms
e.g.
{if $condition}
<p>Some information:</p>
<ul>
{foreach from=$myvars key=key item=value}
<li>{$key} is {$value}</li>
{/foreach}
</ul>
{/if}
Readable in both PHP and HTML terms.
However there are further considerations when it comes to PHP.
Sometimes you just want to display a small bit of text and this bit
will be displayed several times - maybe as part of a loop. You have
to be careful that you don't reload (re-include) the template page
every time the snippet is displayed or your performance tanks. In
this case I would tend to use ECHO statements over dropping out of
PHP (e.g. ?> <p>my test</p> <? ) because when there is just a small
amount of HTML code inside your PHP function, the readability of the
PHP is more important than the readability of the HTML.
Anyways, that is just my take on the issue after years of trying it
every way under the sun.
Best regards
Steve
> I’ll put all parsing logic at the top of a page, above any HTML
> code, and try to only drop into PHP when I need to print db data or
> other logic-based tasks. For example, if I’m not using PHP to write
> any variables into an element on a page, then (unless I’m using a
> loop to write out a table) I won’t use PHP to do it.
>
> For my money...
>
> <table>
> <?php
> // code to write out table rows and cells
> ?>
> </table>
>
> ...is cleaner, more elegant and easier to read than needlessly
> getting PHP to write out HTML elements.
>
> --
> Kevin Futter
> Webmaster, St. Bernard's College
> http://www.sbc.melb.catholic.edu.au/
>
> ------------------------------------------
> This e-mail and any attachments may be confidential. You must not
> disclose or use the information in this e-mail if you are not the
> intended recipient. If you have received this e-mail in error,
> please notify us immediately and delete the e-mail and all copies.
> The College does not guarantee that this e-mail is virus or error
> free. The attached files are provided and may only be used on the
> basis that the user assumes all responsibility for any loss, damage
> or consequence resulting directly or indirectly from the use of the
> attached files, whether caused by the negligence of the sender or
> not. The content and opinions in this e-mail are not necessarily
> those of the College.
> _______________________________________________
> FX.php_List mailing list
> FX.php_List at mail.iviking.org
> http://www.iviking.org/mailman/listinfo/fx.php_list
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.iviking.org/pipermail/fx.php_list/attachments/20060720/9eaac6ef/attachment.html
More information about the FX.php_List
mailing list