|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2003-08-05 05:28 UTC] nickj-php at nickj dot org
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.
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Sun Nov 02 00:00:01 2025 UTC |
That there is no way to write a function to handle unset vars is also something I have come across. One solution would be to use the @-symbol: print noUnset(@$x, "not set"); However, the @-symbol is not guaranteed to work, since a user-defined error handler will ignore it. Only isset(), unset(), and empty() can handle undefined variables without implicitly declaring them. Perhaps a new construct can be made available to handle this common case. default($var['not_a_key'],"default_value") => "default_value" It would work like the following function, but using the caller environment (which could be from within another function): function default($var,$default="") { return isset($var)?$var:$default; } Using "" for the inherent default value makes sense for web interfaces.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'); ?>">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)