#39553 [NEW]: Add coalesce() and coalesce_strict() functions

From: Date: Sun, 19 Nov 2006 09:36:39 +0000
Subject: #39553 [NEW]: Add coalesce() and coalesce_strict() functions
Groups: php.bugs 
Request: Send a blank email to php-bugs+get-105183@lists.php.net to get a copy of this message
From:             greg at mtechsolutions dot ca
Operating system: all
PHP version:      5.2.0
PHP Bug Type:     Feature/Change Request
Bug description:  Add coalesce() and coalesce_strict() functions

Description:
------------
I see value in adding two functions to php: coalesce() and
coalesce_strict(). 

Both of these would take an arbitrary number of arguments, and use the
first non-empty() or non-null value (respectively).

For example:

$username = coalesce($_POST['username'], $_COOKIE['username'],
'guest');

Parameters passed would not have to be defined (eg, the above script
should not generate notices if E_STRICT is on and $_POST['username'] is
undefined), and undefined variables would be treated as null. 


Reproduce code:
---------------
// The PHP (close) equivalents:
function coalesce() {
	$max = func_num_args();
	for ($i = 0; $i < $max-1; $i++) {
		$value = func_get_arg($i);
		if (!empty($value)) {
			return $value;
		}
	}
	return func_get_arg($max-1);
}
function coalesce_strict() {
	$max = func_num_args();
	for ($i = 0; $i < $max-1; $i++) {
		$value = func_get_arg($i);
		if ($value !== null) {
			return $value;
		}
	}
	return func_get_arg($max-1);
}

Expected result:
----------------
coalesce('',0,1); // returns 1
coalesce(0,null,false,''); // returns '' (last value)
coalesce_strict(0,null,false,''); // returns 0




-- 
Edit bug report at http://bugs.php.net/?id=39553&edit=1
-- 
Try a CVS snapshot (PHP 4.4): http://bugs.php.net/fix.php?id=39553&r=trysnapshot44
Try a CVS snapshot (PHP 5.2): http://bugs.php.net/fix.php?id=39553&r=trysnapshot52
Try a CVS snapshot (PHP 6.0): http://bugs.php.net/fix.php?id=39553&r=trysnapshot60
Fixed in CVS:                 http://bugs.php.net/fix.php?id=39553&r=fixedcvs
Fixed in release:             http://bugs.php.net/fix.php?id=39553&r=alreadyfixed
Need backtrace:               http://bugs.php.net/fix.php?id=39553&r=needtrace
Need Reproduce Script:        http://bugs.php.net/fix.php?id=39553&r=needscript
Try newer version:            http://bugs.php.net/fix.php?id=39553&r=oldversion
Not developer issue:          http://bugs.php.net/fix.php?id=39553&r=support
Expected behavior:            http://bugs.php.net/fix.php?id=39553&r=notwrong
Not enough info:              http://bugs.php.net/fix.php?id=39553&r=notenoughinfo
Submitted twice:              http://bugs.php.net/fix.php?id=39553&r=submittedtwice
register_globals:             http://bugs.php.net/fix.php?id=39553&r=globals
PHP 3 support discontinued:   http://bugs.php.net/fix.php?id=39553&r=php3
Daylight Savings:             http://bugs.php.net/fix.php?id=39553&r=dst
IIS Stability:                http://bugs.php.net/fix.php?id=39553&r=isapi
Install GNU Sed:              http://bugs.php.net/fix.php?id=39553&r=gnused
Floating point limitations:   http://bugs.php.net/fix.php?id=39553&r=float
No Zend Extensions:           http://bugs.php.net/fix.php?id=39553&r=nozend
MySQL Configuration Error:    http://bugs.php.net/fix.php?id=39553&r=mysqlcfg


Thread (3 messages)

« previous php.bugs (#105183) next »