Populating an HTML combo box by looped concatenation This code takes the results of a database query and uses them to populate the options in a combobox on a form.

Would it be better to start with an empty htmlCode and then concatenate all the &lt;option&gt; code in the loop before finally using echo outside the loop?

Also, do I need to declare $boxOption, $paramKey and $paramValue as class variables or are they OK as is because they're only used in this method?

public function populateComboBox ($boxToPopulate)
    {
        $this-&gt;boxOptions = $this-&gt;getQueryResults($this-&gt;boxParams[$boxToPopulate]['query']);

        foreach ($this-&gt;boxOptions as $boxOption) {

            foreach ($this-&gt;htmlParams as $paramKey =&gt; $paramValue) {

                if (!empty($this-&gt;boxParams[$boxToPopulate][$paramKey])) {

                    $this-&gt;htmlParams[$paramKey] = $boxOption[$this-&gt;boxParams[$boxToPopulate][$paramKey]];
                }
            }

            $this-&gt;htmlCode = "&lt;option value='{$this-&gt;htmlParams['value']}' "
                              . "class='{$this-&gt;htmlParams['class']}'&gt;"
                              . "{$this-&gt;htmlParams['text']}&lt;/option&gt;\n";

            echo &lt;&lt;&lt;_CODE
                                {$this-&gt;htmlCode}
_CODE;
        }

    }


I use an array for each combobox on the form:

    $this-&gt;boxParams['issue'] = array (
        'query' =&gt; 'SELECT interface_issue, current_issue '
                 . 'FROM interface_issue '
                 . 'WHERE current_issue = -1 '
                 . 'ORDER BY interface_issue;',
        'value' =&gt; 'interface_issue',
        'class' =&gt; '',
        'text' =&gt; 'interface_issue'
    );
You give us a very short piece of code, and two simple questions. I will not address the other issues of your code (or maybe I will), but concentrate on the two questions.

1. "Would it be better to start with an empty htmlCode and then concatenate all the  code in the loop before finally using echo outside the loop?"

I guess the other alternative is echo'ing the html inside the loop? This is probably a question about performance? The best way to figure this out, is to test both cases. If the question is about coding I would not echo inside this method at all, but just return a string with the HTML. This allow you to decide later, in another part of the code, what to do with the HTML: Store it? Put it in an email? Or echo it?

2. "do I need to declare $boxOption, $paramKey and $paramValue as class variables or are they OK as is because they're only used in this method?".

Well, if they're only used in this method, then they should be local variables, NOT the class variables which you made them into now. So replace $this-&gt; by $.

Just one remark about your method: A method should have one purpose, one thing to do. Yours does several: 1. retrieve data from the database using a query that's stored in a strange way. 2. Build HTML, but not even a whole &lt;select&gt;, only the options. 3. Output the HTML. This is not very flexible. Your data always has to come from a predetermined place, and your output will always be echoed. Any variation on this method will require a completely new method, probably doing almost the same thing.

You also seem to have a class that does many things. The 'single responsibility principle' doesn't seem to have been applied. These principles are here for a very good reason. See:

http://en.wikipedia.org/wiki/SOLID_%28object-oriented_design%29

A programmer can ignore these principles for a long time, and programs will work, but when they grow, and expand, you will find that sticking to these principles can be very useful. Luckily the 'single responsibility principle' is one of the easiest to understand. A better explanation can be found here:

http://www.codemag.com/article/1001061