Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

« Newer Snippets
Older Snippets »
Showing 1-2 of 2 total  RSS 

Value coalescence in PHP

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.
function coalesce() {
    $args = func_get_args();
    foreach ($args as $arg) {
        if (!empty($arg)) {
            return $arg;
        }
    }
    return $args[0];
}

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.

Delete empty directories (UNIX)

// Shell command to delete empty directories. May have to run several times to get everything.

find . -type d -empty | xargs rmdir -
« Newer Snippets
Older Snippets »
Showing 1-2 of 2 total  RSS