Edit report at http://bugs.php.net/bug.php?id=24949&edit=1
ID: 24949 Updated by: j...@php.net Reported by: nickj-php at nickj dot org Summary: Requesting nicer way of setting undefined variables to default val. ifsetor / ?: -Status: Open +Status: Closed Type: Feature/Change Request -Package: Feature/Change Request +Package: *General Issues -Operating System: Any +Operating System: * -PHP Version: PHP6 +PHP Version: 6 -Assigned To: +Assigned To: jani Block user comment: N Private report: N New Comment: ?: exists since 5.3. Previous Comments: ------------------------------------------------------------------------ [2006-01-20 01:46:25] nickj-php at nickj dot org That workaround works for most situations, but maybe not all, such as for constants (which cannot be passed-by-reference): $val = _ifsetor(null, 'some value'); $php_ver = _ifsetor(PHP_VERSION, 'some value'); However, I'm not sure whether such a thing should ideally work (i.e. perhaps it's best that this doesn't work, but on the other hand I can't see why it shouldn't) - so I'm honestly not sure. The main point though is that it's a common requirement, especially for web forms, to have to test for a set value, and supply a default if none is set - so it could be good if the language core natively included this functionality. Happily, it looks like this is going to happen in PHP6. Please see: http://www.php.net/~derick/meeting-notes.html#ifsetor-as-replacement-for-foo-isset-foo-foo-something-else (long URL, so may have wraparound issues). The outcome was that PHP will get a new "?:" construct, like the one mentioned previously by Xuefer. Personally, I realise now that this is a far neater approach that having a function (such as ifsetor/noUnset/default), so kudos to the PHP developers. ------------------------------------------------------------------------ [2006-01-19 19:03:01] chris dot vigelius at gmx dot net Workaround: You can implement the functionality using references error_reporting (E_ALL); function _ifsetor(&$field, $defaultvalue) { return (isset($field))?$field:$defaultvalue; } $v1 = $v2 = 'x'; echo _ifsetor($v1, 'v1 is not set'); unset($v2); echo _ifsetor($v2, 'v2 is not set'); This works in 5.1.2 without warnings (haven't checked other versions, though) ------------------------------------------------------------------------ [2005-01-02 02:42:02] nickj-php at nickj dot org >From Derick Rethans PHP Look Back 2004 [http://www.derickrethans.nl/month-2004-12.php?item=20041231#20041231], there were two discussions during the year on this topic on the PHP-DEV mailing list, which I am adding links to here (as they help to summarize various aspects of this) : 1) April 2004: http://groups.google.com.au/groups?threadm=c5m94m$31k...@freebsd.csie.nctu.edu.tw 2) July 2004: http://groups.google.com.au/groups?threadm=cci54b$t6...@freebsd.csie.nctu.edu.tw ------------------------------------------------------------------------ [2003-10-09 18:45:32] marcus at deck16 dot com I also came across that "problem" and voted for that "bug". It is possible to write a function though. Just pass the var Name as a string: <input type="text" name="Email" size="24" value="<?php echo Set_Form_Value('Email'); ?>"> ------------------------------------------------------------------------ [2003-08-22 12:00:36] xuefer at 21cn dot com it would be nice to use "?:" operator as new c++ language $a = isset($a) ? $a : ""; -> $a = $a ?: ""; $a = $a ?: $b ?: $c ?: $d; better than: $a = default($a, $b, $c, $d); ------------------------------------------------------------------------ The remainder of the comments for this report are too long. To view the rest of the comments, please view the bug report online at http://bugs.php.net/bug.php?id=24949 -- Edit this bug report at http://bugs.php.net/bug.php?id=24949&edit=1