ID:               24949
 User updated by:  nickj-php at nickj dot org
-Summary:          Requesting nicer way of setting undefined variables to
                   a default value - ifsetor
 Reported By:      nickj-php at nickj dot org
 Status:           Open
 Bug Type:         Feature/Change Request
 Operating System: Any
-PHP Version:      Irrelevant
+PHP Version:      PHP6
 New Comment:

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.


Previous Comments:
------------------------------------------------------------------------

[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/[EMAIL PROTECTED]
2) July 2004:
http://groups.google.com.au/[EMAIL PROTECTED]

------------------------------------------------------------------------

[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);

------------------------------------------------------------------------

[2003-08-18 21:27:38] hurst at webteks dot com

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.

------------------------------------------------------------------------

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/24949

-- 
Edit this bug report at http://bugs.php.net/?id=24949&edit=1

Reply via email to