Re: Function proposal: varset
| From: | Mark | Date: | Wed, 20 Apr 2011 22:50:46 +0000 |
| Subject: | Re: Function proposal: varset | ||
| References: | 1 2 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-51966@lists.php.net to get a copy of this message | ||
On Wednesday, April 20, 2011, D. Dante Lorenso <dante@lorenso.com> wrote:
> On 4/20/11 9:55 AM, Mark wrote:
>
> Hi,
> This proposal is for the often called line like this:
> $var = isset($_GET['var']) ? $_GET['var'] : '';
> Only a shorter and imho a cleaner solution to get the same:
> $var = varset($_GET['var']);
>
> The implementation for this in PHP code is this:
>
> # Arg 1 = the variable to check for existence
> # Arg 2 = the default return value which is an empty string by default
>
> function varset($var, $default = '')
> {
> return (isset($var) ? $var : $default);
> }
>
>
> I proposed something similar over 5 years ago. It ain't gonna happen because PHP
> language can't support it. The Zend engine needs to be rewritten to remove the warnings and
> that's not something they are volunteering to do no matter how much people want it.
>
> http://markmail.org/message/yl26ebzcix35wtke
>
> My proposal was called "filled" since it was the opposite of "empty" which
> already existed. I extended the function to return the first non-empty value or null if all values
> evaluated as empty.
>
> You could use the function like this:
>
> $x = filled($_GET['x'], $obj->something, $default, 'default');
>
> It would return the first argument where !empty($arg) evaluates as TRUE.
>
> There would need to be a companion function to check 'isset' opposite as you have
> proposed.
>
> Like I said, though, I don't think this can be done in the language because I think they
> ran out of OPCODES and would have to tear apart the whole PHP engine to support such a feature.
>
> -- Dante
>
> --
> PHP Internals - PHP Runtime Development Mailing List
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>
Hi,
well, i did propose 2 possible ways although the second one is a
little longer but still clean and readable.
And the second one is certainly possible to implement!
Btw. i did look at the suggested core php code parts and even though i
can do nearly everything in php.. i have a hard time understanding how
the inner php parsing things actually work. There is no way i'm able
to make a patch for php.
Regards,
Mark