By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,737 Members | 1,917 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,737 IT Pros & Developers. It's quick & easy.

Is this bad coding practice?

P: n/a
After recently coming back to php after working in asp for several
years, I want to do things well, so I'm very concerned with learning
good coding practices.

Also I'm trying to seperate logic from presentation as much as I can
through the use of the <?=$variable_output_goes_here?> output. I'll
give you an example.

Instead of putting the body of a for loop (that displays info from an
array in a table), in the html, I'll declare a variable and put
everything in the variable.

<?php

$display_results = '<table>';

for ($i = 0; $i < 3; $i++) {
$display_results .= '<tr><td>' . $info[$i][0] . '</td><td>' .
$info[$i][1] . '</td><td>' . $info[$i][2] . '</td></tr>';
}

$display_results .= '</table>';

?>

Then I'll output into the html like so . . .

<?=$display_results?>

My question is, even though this seems to work, now I'm learning on a
small scale at home. If I take this sort of coding habit to a
production enviornment, is it going to slow down my code if I need to
scale something up? If I'm this interested in logic/presentation
seperation, would I just be better off using smarty?

Thanks,
W
Jul 17 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a

"Eakin, W" <da***@euroweb.com> wrote in message
news:b3**************************@posting.google.c om...
After recently coming back to php after working in asp for several
years, I want to do things well, so I'm very concerned with learning
good coding practices.

Also I'm trying to seperate logic from presentation as much as I can
through the use of the <?=$variable_output_goes_here?> output. I'll
give you an example.

Instead of putting the body of a for loop (that displays info from an
array in a table), in the html, I'll declare a variable and put
everything in the variable.

<?php

$display_results = '<table>';

for ($i = 0; $i < 3; $i++) {
$display_results .= '<tr><td>' . $info[$i][0] . '</td><td>' .
$info[$i][1] . '</td><td>' . $info[$i][2] . '</td></tr>';
}

$display_results .= '</table>';

?>

Then I'll output into the html like so . . .

<?=$display_results?>

My question is, even though this seems to work, now I'm learning on a
small scale at home. If I take this sort of coding habit to a
production enviornment, is it going to slow down my code if I need to
scale something up? If I'm this interested in logic/presentation
seperation, would I just be better off using smarty?

Thanks,
W


If you want to separate presentation from business logic you are talking
about the 3 tier architecture (take a look at
http://www.tonymarston.co.uk/php-mys...tructure.html), or possibly the
Model-View-Controller design pattern (take a look at
http://www.tonymarston.co.uk/php-mys...tructure.html).

This is where HTML templating engines come into play for dealing with
presentation. If you want one which is based around PHP then Smarty is
widely used. Personally I prefer a much more powerful and widely acceptable
engine, which is why I use XML and XSL transformations. These are standards
supported by the World Wide Web Consortium and are accessible by any
language, not just PHP.

Take a look at http://www.tonymarston.net/php-mysql...plication.html
for a sample application.

--
Tony Marston

http://www.tonymarston.net

Jul 17 '05 #2

P: n/a

"Eakin, W" <da***@euroweb.com> wrote in message
news:b3**************************@posting.google.c om...
My question is, even though this seems to work, now I'm learning on a
small scale at home. If I take this sort of coding habit to a
production enviornment, is it going to slow down my code if I need to
scale something up? If I'm this interested in logic/presentation
seperation, would I just be better off using smarty?


Yoda's answer would be "There is no such bad coding practice--just bad
code."

PHP.net itself does something similiar, so I don't think anyone would call
it bad practice.

Personally I do a lot of this

<table>
<? for ($i = 0; $i < 3; $i++): ?>
<tr>
<td><?=$info[$i][0]?></td>
<td><?=$info[$i][1]?></td>
<td><?=$info[$i][2]?></td>
</tr>
<? endfor; ?>
</table>

Makes it easier to build and debug complicated layouts.
Jul 17 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.