Is this bad coding practice?

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
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.


$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 . . .


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?


Re: Is this bad coding practice?

Quoted text here. Click to load it

If you want to separate presentation from business logic you are talking
about the 3 tier architecture (take a look at ), or possibly the
Model-View-Controller design pattern (take a look at ).

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
for a sample application.

Tony Marston

Re: Is this bad coding practice?

Quoted text here. Click to load it

Yoda's answer would be "There is no such bad coding practice--just bad
code." itself does something similiar, so I don't think anyone would call
it bad practice.

Personally I do a lot of this

<? for ($i = 0; $i < 3; $i++): ?>
<? endfor; ?>

Makes it easier to build and debug complicated layouts.

Site Timeline