<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: php code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Fri, 10 Oct 2008 23:57:40 GMT</pubDate>
    <description>DZone Snippets: php code</description>
    <item>
      <title>Flattening an array of arrays in PHP</title>
      <link>http://snippets.dzone.com/posts/show/4660</link>
      <description>It does just what it says on the tin:&lt;br /&gt;&lt;code&gt;&lt;br /&gt;/** Denests the nested arrays within the given array. */&lt;br /&gt;function flatten_array(array $a) {&lt;br /&gt;    $i = 0;&lt;br /&gt;    while ($i &lt; count($a)) {&lt;br /&gt;        if (is_array($a[$i])) {&lt;br /&gt;            array_splice($a, $i, 1, $a[$i]);&lt;br /&gt;        } else {&lt;br /&gt;            $i++;&lt;br /&gt;        }&lt;br /&gt;    }&lt;br /&gt;    return $a;&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;With added unit-testy goodness:&lt;br /&gt;&lt;code&gt;&lt;br /&gt;class FlattenTest extends PHPUnit_Framework_TestCase {&lt;br /&gt;&lt;br /&gt;    public function test_flatten() {&lt;br /&gt;        $this-&gt;assertEquals(array(), flatten_array(array()));&lt;br /&gt;        $this-&gt;assertEquals(array(1), flatten_array(array(1)));&lt;br /&gt;        $this-&gt;assertEquals(array(1), flatten_array(array(array(1))));&lt;br /&gt;        $this-&gt;assertEquals(array(1, 2), flatten_array(array(array(1, 2))));&lt;br /&gt;        $this-&gt;assertEquals(array(1, 2), flatten_array(array(array(1), 2)));&lt;br /&gt;        $this-&gt;assertEquals(array(1, 2), flatten_array(array(1, array(2))));&lt;br /&gt;        $this-&gt;assertEquals(array(1, 2, 3), flatten_array(array(1, array(2), 3)));&lt;br /&gt;        $this-&gt;assertEquals(array(1, 2, 3, 4), flatten_array(array(1, array(2, 3), 4)));&lt;br /&gt;    }&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Tue, 16 Oct 2007 08:30:23 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4660</guid>
      <author>keith (Keith Gaughan)</author>
    </item>
    <item>
      <title>Value coalescence in PHP</title>
      <link>http://snippets.dzone.com/posts/show/3664</link>
      <description>Here's something I keep on having to do in PHP and other languages. Often I need to find the first non-empty value from a set of arguments. Here's how I do it.&lt;br /&gt;&lt;code&gt;&lt;br /&gt;function coalesce() {&lt;br /&gt;    $args = func_get_args();&lt;br /&gt;    foreach ($args as $arg) {&lt;br /&gt;        if (!empty($arg)) {&lt;br /&gt;            return $arg;&lt;br /&gt;        }&lt;br /&gt;    }&lt;br /&gt;    return $args[0];&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;The function assumes that it will be called with at least one value. It can be trivially altered to use is_null() instead if that suits.</description>
      <pubDate>Tue, 13 Mar 2007 11:00:32 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/3664</guid>
      <author>keith (Keith Gaughan)</author>
    </item>
    <item>
      <title>An analogue of ColdFusion's CFPARAM tag in PHP.</title>
      <link>http://snippets.dzone.com/posts/show/492</link>
      <description>&lt;code&gt;&lt;br /&gt;/**&lt;br /&gt; * Checks if $var is already defined and if not, sets it to&lt;br /&gt; * $default.&lt;br /&gt; *&lt;br /&gt; * @param  $var         Variable to check.&lt;br /&gt; * @param  $default     Value to give the checked variable if&lt;br /&gt; *                      found to be null; "" by default.&lt;br /&gt; * @param  $catchNulls  TRUE: treat empty strings as null; FALSE&lt;br /&gt; *                      by default.&lt;br /&gt; *&lt;br /&gt; * @note   It's a lot like ColdFusion's &lt;CFPARAM&gt; tag.&lt;br /&gt; */&lt;br /&gt;function set_default(&amp;$var, $default="", $catchNulls=FALSE) {&lt;br /&gt;    if (!isset($var) || ($catchNulls &amp;&amp; $var == "")) {&lt;br /&gt;        $var = $default;&lt;br /&gt;    }&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;This appears to have stopped working as of PHP 5.</description>
      <pubDate>Sat, 23 Jul 2005 03:14:00 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/492</guid>
      <author>keith (Keith Gaughan)</author>
    </item>
    <item>
      <title>The world's simplest (decent) PHP templating engine...</title>
      <link>http://snippets.dzone.com/posts/show/489</link>
      <description>...is PHP, so why not use it?&lt;br /&gt;&lt;code&gt;&lt;br /&gt;function include_view($__view, $vars=NULL) {&lt;br /&gt;    # Start buffering the generated text.&lt;br /&gt;    ob_start();&lt;br /&gt;&lt;br /&gt;    # Process the view.&lt;br /&gt;    if (!is_null($vars)) {&lt;br /&gt;        extract($vars, EXTR_OVERWRITE | EXTR_REFS);&lt;br /&gt;    }&lt;br /&gt;    include("views/$__view.php");&lt;br /&gt;&lt;br /&gt;    # Grab the generated content and clean up.&lt;br /&gt;    $content = ob_get_contents();&lt;br /&gt;    ob_end_clean();&lt;br /&gt;&lt;br /&gt;    return $content;&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Fri, 22 Jul 2005 07:02:14 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/489</guid>
      <author>keith (Keith Gaughan)</author>
    </item>
  </channel>
</rss>
