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

 ID:                 24949
 Comment by:         ken at smallboxcms dot com
 Reported by:        nickj-php at nickj dot org
 Summary:            Requesting nicer way of setting undefined variables
                     to default val. ifsetor / ?:
 Status:             Closed
 Type:               Feature/Change Request
 Package:            *General Issues
 Operating System:   *
 PHP Version:        6
 Assigned To:        jani
 Block user comment: N
 Private report:     N

 New Comment:

I personally have never found a use for ?: and think at best it rarely
would solve the problem that ifsetor has been requested for. because ?:
throws a notice when the variable being tested for is undefined one is
still forced to use the longer sintax



$foo = isset($_REQUEST['name_of_really_long_variable']) ?
$_REQUEST['name_of_really_long_variable'] : null;



Hands down my biggest peeve with PHP



Also sadly there is no way to implement something in userland because
testing for a new element in an array has the unfortunate side effect of
also setting that element. Only solution is an operator that works like
isset or empty but has the potential to return the value of the variable
being tested for instead of true or false.


Previous Comments:
------------------------------------------------------------------------
[2010-11-18 23:08:00] [email protected]

?: exists since 5.3.

------------------------------------------------------------------------
[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/[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'); ?>">

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


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

Reply via email to