From: nickj-php at nickj dot org Operating system: Any PHP version: Irrelevant PHP Bug Type: Feature/Change Request Bug description: Requesting a nicer way of setting undefined variables to a default value.
Description: ------------ Requesting a nicer way of setting undefined variables to a default value. It gets a bit repetitive and ugly having to use statements like: $title = isset($vals["title"]) ? $vals["title"] : ""; $first = isset($vals["first_name"]) ? $vals["first_name"] : ""; $surname = isset($vals["surname"]) ? $vals["surname"] : ""; This seems to be a recurrent requirement, so ideally there would be some way of doing this, along the lines of : function noUnset($val, $default="") { return isset($val) ? $val : $default; } Unfortunately a user function such as the above cannot be defined to do this, since the first argument passed to the function would be unset. For example, this code will generate an error: ========================================== error_reporting (E_ALL); function noUnset($val, $default="") { return isset($val) ? $val : $default; } if (isset($x)) unset ($x); // $x = 2; // works OK if this line is uncommented print noUnset($x, "not set"); // Above line generates an error as noUnset cannot be called with $x, because it is not set - catch-22 ! ========================================== This code generates an "Undefined variable: x" error. Ideally there would be slightly nicer way of setting defaults than a bank is "isset" statements, that would work without generating an error, even with all error reporting options turned on. -- Edit bug report at http://bugs.php.net/?id=24949&edit=1 -- Try a CVS snapshot (php4): http://bugs.php.net/fix.php?id=24949&r=trysnapshot4 Try a CVS snapshot (php5): http://bugs.php.net/fix.php?id=24949&r=trysnapshot5 Fixed in CVS: http://bugs.php.net/fix.php?id=24949&r=fixedcvs Fixed in release: http://bugs.php.net/fix.php?id=24949&r=alreadyfixed Need backtrace: http://bugs.php.net/fix.php?id=24949&r=needtrace Try newer version: http://bugs.php.net/fix.php?id=24949&r=oldversion Not developer issue: http://bugs.php.net/fix.php?id=24949&r=support Expected behavior: http://bugs.php.net/fix.php?id=24949&r=notwrong Not enough info: http://bugs.php.net/fix.php?id=24949&r=notenoughinfo Submitted twice: http://bugs.php.net/fix.php?id=24949&r=submittedtwice register_globals: http://bugs.php.net/fix.php?id=24949&r=globals PHP 3 support discontinued: http://bugs.php.net/fix.php?id=24949&r=php3 Daylight Savings: http://bugs.php.net/fix.php?id=24949&r=dst IIS Stability: http://bugs.php.net/fix.php?id=24949&r=isapi Install GNU Sed: http://bugs.php.net/fix.php?id=24949&r=gnused