A quick way to handle both strings and arrays in PHP

Something that happens occasionally is that I need to create a function that will specifically output chunks of HTML.

A good method is to allow for functions to handle both single strings and arrays. The problem? I don’t like duplicating code. The solution? A quick convention to an array!

function arrayish($a)
{
  $a = (array) $a;

  foreach ($a as $b) {
    echo "<p>$b</p>";
  }
}

That’s it. Nice and simple!

If someone knows a more efficient way to do this, I’d love to know.