<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: coalesce code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Sat, 06 Sep 2008 14:40:05 GMT</pubDate>
    <description>DZone Snippets: coalesce code</description>
    <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>
  </channel>
</rss>
