ID:               24949
 Comment by:       marcus at deck16 dot com
 Reported By:      nickj-php at nickj dot org
 Status:           Open
 Bug Type:         Feature/Change Request
 Operating System: Any
 PHP Version:      Irrelevant
 New Comment:

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


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

[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.

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

[2003-08-05 05:28:19] 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.



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


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

Reply via email to