Add HTML to the page using a PHP Class I was trying to make my life easier making a class or something that would create HTML tags in a faster way. I ended up with something that I wouldn't consider the best practice. Take a look:

/**
 * Add HTML to the page
 *
 * Add HTML to the page based on arrays with the tag names. It's gonna open and close the tags.
 *
 * @param array tags The name of the tags
 * @param array attributes The attributes of the tags if you want
 * @param array The elements you want inside the tags
 */
function addHtml($tags = null, $attributes = null, $innerHtml = null)
{
    for($i = 0; $i &lt; count($tags); $i++) {
        // Opens the tag
        echo '&lt;' . $tags[$i];

        // If there're attribures, show them and close
        // If not, just closes the tag
        echo !empty($attributes) ? ' ' . $attributes[$i] . '&gt;' : '&gt;';

        // Show something you wanna put inside the tag. Another tag, text, anything
        echo $innerHtml[$i];

        // Closes the tag
        echo '&lt;/' . $tags[$i] . '&gt;';
    }       
}


Is there someone with a better way of doing it?
When it comes to outputting HTML from PHP, there are a couple of alternatives:


Using a MVC framework which will separate the logic (Controller) from the data (Model) and the output (View).
Include files that contains HTML, and that can use some PHP as well ("Templates"). MVC frameworks normally makes use of this.
For short HTML, I believe there is nothing that beats using PHP's echo function and write the HTML you want manually.


A big problem with your current function is that to output several HTML tags, you'd have to write things that actually belongs together very far apart. For example: 

addHTML(array("p", "p"), 
  array('class="some"', 'class="other"'), 
  array("Lorem ipsum", "dolor sit amet"));


It's not very readable to see that one tag will become &lt;p class="some"&gt;Lorem ipsum&lt;/p&gt; and the other &lt;p class="other"&gt;dolor sit amet&lt;/p&gt;.

If you really really want to have some self-written PHP function for this, then I suggest putting things that belong together, together. Something like this:

addHTML(array(array("p", "class" =&gt; "some", "Lorem ipsum"), 
             array("p", "class" =&gt; "other", "dolor sit amet")));


However, simply writing this would be so much simpler, and a whole lot easier to read: (Especially for people who are used to reading HTML)

echo '&lt;p class="some"&gt;Lorem ipsum&lt;/p&gt;';
echo '&lt;p class="other"&gt;dolor sit amet&lt;/p&gt;';


(My PHP is a bit rusty but I hope you get the idea)