FAQ
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);
}

However there is a slight issue with this approach. If notices are turned on
this code will generate a notice while i think it should not do that. But
perhaps this approach is "to short".
A slightly different implementation (and longer) prevents the notice:

# Arg 1 = the array in which a given key should be checked for existence
# Arg 2 = the key to check in the array from arg 1
# Arg 3 = the default return value which is an empty string by default

function varset($arr, $key, $default = '')
{
return (isset($arr[$key]) ? $arr[$key] : $default);
}

where the call would be:
$var = varset($_GET, 'var');

I personally like both ways...
My proposal is to make this function a core php function in PHP 5.4. The
added benifit is obvious. People can, with this, use a way shorter notation
to validate if a given array element exists. Right now that needs to be done
with a ternary, filter_var or some other method (there are quite a few ways
to check for existence).

I tried to look in the PHP source to see if i could make a patch to add this
but i couldn't find the function that defines the isset function (i wanted
to base it on that). So a pointer to the right location would be nice (along
with docs that tell me what i all need to do to implement a new php
function). If someone else wants to make the implementation, please be my
guest :)

So, what do you think of this?

Regards,
Mark

Search Discussions

  • Alain Williams at Apr 20, 2011 at 3:12 pm

    On Wed, Apr 20, 2011 at 04:55:00PM +0200, 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']);
    It should be called var_set() - better on name space pollution.
    However there is a slight issue with this approach. If notices are turned on
    this code will generate a notice while i think it should not do that. But
    perhaps this approach is "to short".
    A slightly different implementation (and longer) prevents the notice:
    If is is a language element (like isset()) then you can avoid this problem.

    I do find a lot of code, in simple scripts, that does just that.

    It might be nice to extend it such that if the 1st argument is a list then the
    first in the list which is set is returned, eg:

    $var = var_set(($_GET['var'], $_POST['var']), 'default');

    --
    Alain Williams
    Linux/GNU Consultant - Mail systems, Web sites, Networking, Programmer, IT Lecturer.
    +44 (0) 787 668 0256 http://www.phcomp.co.uk/
    Parliament Hill Computers Ltd. Registration Information: http://www.phcomp.co.uk/contact.php
    #include <std_disclaimer.h>
  • Mark at Apr 20, 2011 at 3:20 pm

    On Wed, Apr 20, 2011 at 5:12 PM, Alain Williams wrote:
    On Wed, Apr 20, 2011 at 04:55:00PM +0200, 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']);
    It should be called var_set() - better on name space pollution.
    oke

    However there is a slight issue with this approach. If notices are turned on
    this code will generate a notice while i think it should not do that. But
    perhaps this approach is "to short".
    A slightly different implementation (and longer) prevents the notice:
    If is is a language element (like isset()) then you can avoid this problem.
    Could you explain that a bit more?

    I do find a lot of code, in simple scripts, that does just that.

    It might be nice to extend it such that if the 1st argument is a list then
    the
    first in the list which is set is returned, eg:

    $var = var_set(($_GET['var'], $_POST['var']), 'default');
    I might be missing the point here, but the way i understand it that can give
    unexpected results.. since it returns the first element from an array in
    your suggestion but that's not what you want to do.

    --
    Alain Williams
    Linux/GNU Consultant - Mail systems, Web sites, Networking, Programmer, IT
    Lecturer.
    +44 (0) 787 668 0256 http://www.phcomp.co.uk/
    Parliament Hill Computers Ltd. Registration Information:
    http://www.phcomp.co.uk/contact.php
    #include <std_disclaimer.h>
  • Alain Williams at Apr 20, 2011 at 3:32 pm

    On Wed, Apr 20, 2011 at 05:19:36PM +0200, Mark wrote:

    If is is a language element (like isset()) then you can avoid this problem.
    Could you explain that a bit more?
    It looks like a function but is not:

    http://uk3.php.net/manual/en/function.isset.php
    It might be nice to extend it such that if the 1st argument is a list then
    the
    first in the list which is set is returned, eg:

    $var = var_set(($_GET['var'], $_POST['var']), 'default');
    I might be missing the point here, but the way i understand it that can give
    unexpected results.. since it returns the first element from an array in
    your suggestion but that's not what you want to do.
    I mean that it checks $_GET['var'], then $_POST['var'] & returns the first of the two that is set
    or 'default'.

    --
    Alain Williams
    Linux/GNU Consultant - Mail systems, Web sites, Networking, Programmer, IT Lecturer.
    +44 (0) 787 668 0256 http://www.phcomp.co.uk/
    Parliament Hill Computers Ltd. Registration Information: http://www.phcomp.co.uk/contact.php
    #include <std_disclaimer.h>
  • Brian Moon at Apr 20, 2011 at 4:06 pm

    It might be nice to extend it such that if the 1st argument is a list then the
    first in the list which is set is returned, eg:

    $var = var_set(($_GET['var'], $_POST['var']), 'default');
    If that is the usage, I would suggest coalesce() to be consistent with
    the same concept in SQL. And you would not need a list as the first
    argument. Just pass multiple arguments and return the first set value.

    $var = var_set($_GET['var'], $_POST['var'], 'default');

    http://en.wikipedia.org/wiki/Null_%28SQL%29#COALESCE

    Brian.
  • Alain Williams at Apr 20, 2011 at 4:19 pm

    On Wed, Apr 20, 2011 at 11:06:47AM -0500, Brian Moon wrote:
    It might be nice to extend it such that if the 1st argument is a list then
    the
    first in the list which is set is returned, eg:

    $var = var_set(($_GET['var'], $_POST['var']), 'default');
    If that is the usage, I would suggest coalesce() to be consistent with
    the same concept in SQL. And you would not need a list as the first
    argument. Just pass multiple arguments and return the first set value.

    $var = var_set($_GET['var'], $_POST['var'], 'default');
    Even better.
    http://en.wikipedia.org/wiki/Null_%28SQL%29#COALESCE
    --
    Alain Williams
    Linux/GNU Consultant - Mail systems, Web sites, Networking, Programmer, IT Lecturer.
    +44 (0) 787 668 0256 http://www.phcomp.co.uk/
    Parliament Hill Computers Ltd. Registration Information: http://www.phcomp.co.uk/contact.php
    #include <std_disclaimer.h>
  • Hannes Landeholm at Apr 20, 2011 at 5:14 pm
    This discussion is equivalent to the one that we just had. Read the thread
    "[PHP-DEV] Implicit isset/isempty check on short-ternary operator".

    Also the "$var = var_set($_GET['var'], $_POST['var'], 'default');" syntax
    you propose would be equivalent to (as per previous discussion):

    $var = $_GET[?'var'] $: $_POST[?'var'] $: 'default';

    (The syntax might also be [?'var'], ['var'?] or different).

    On 20 April 2011 18:19, Alain Williams wrote:
    On Wed, Apr 20, 2011 at 11:06:47AM -0500, Brian Moon wrote:
    It might be nice to extend it such that if the 1st argument is a list
    then
    the
    first in the list which is set is returned, eg:

    $var = var_set(($_GET['var'], $_POST['var']), 'default');
    If that is the usage, I would suggest coalesce() to be consistent with
    the same concept in SQL. And you would not need a list as the first
    argument. Just pass multiple arguments and return the first set value.

    $var = var_set($_GET['var'], $_POST['var'], 'default');
    Even better.
    http://en.wikipedia.org/wiki/Null_%28SQL%29#COALESCE
    --
    Alain Williams
    Linux/GNU Consultant - Mail systems, Web sites, Networking, Programmer, IT
    Lecturer.
    +44 (0) 787 668 0256 http://www.phcomp.co.uk/
    Parliament Hill Computers Ltd. Registration Information:
    http://www.phcomp.co.uk/contact.php
    #include <std_disclaimer.h>

    --
    PHP Internals - PHP Runtime Development Mailing List
    To unsubscribe, visit: http://www.php.net/unsub.php
  • Ferenc Kovacs at Apr 20, 2011 at 5:29 pm

    On Wed, Apr 20, 2011 at 7:14 PM, Hannes Landeholm wrote:

    This discussion is equivalent to the one that we just had. Read the thread
    "[PHP-DEV] Implicit isset/isempty check on short-ternary operator".
    except that it wouldn't bring new syntax.

    ps: please don't top post if everybody else does bottom or inline posting,
    it's hard to follow.

    Tyrael
  • Jonathan Bond-Caron at Apr 20, 2011 at 4:00 pm

    On Wed Apr 20 10:55 AM, Mark wrote:

    function varset($arr, $key, $default = '') { return (isset($arr[$key])
    ? $arr[$key] : $default); }

    where the call would be:
    $var = varset($_GET, 'var');

    I personally like both ways...
    My proposal is to make this function a core php function in PHP 5.4.
    The added benifit is obvious. People can, with this, use a way shorter
    notation to validate if a given array element exists. Right now that
    needs to be done with a ternary, filter_var or some other method
    (there are quite a few ways to check for existence).

    I tried to look in the PHP source to see if i could make a patch to
    add this but i couldn't find the function that defines the isset
    function (i wanted to base it on that). So a pointer to the right
    location would be nice (along with docs that tell me what i all need
    to do to implement a new php function).
    https://svn.php.net/viewvc/php/php-src/trunk/Zend/zend_language_parser.y?rev
    ision=306938&view=markup

    Look for "isset_variables:", then zend_do_isset_or_isempty

    isset() lives in the parser and requires some advanced knowledge of the
    opcodes (personally I'm not there yet)
    So, what do you think of this?
    I like the idea, it could also be called vardef() or var_default()
  • Mark at Apr 20, 2011 at 6:58 pm

    On Wed, Apr 20, 2011 at 6:00 PM, Jonathan Bond-Caron wrote:
    On Wed Apr 20 10:55 AM, Mark wrote:

    function varset($arr, $key, $default = '') { return (isset($arr[$key])
    ? $arr[$key] : $default); }

    where the call would be:
    $var = varset($_GET, 'var');

    I personally like both ways...
    My proposal is to make this function a core php function in PHP 5.4.
    The added benifit is obvious. People can, with this, use a way shorter
    notation to validate if a given array element exists. Right now that
    needs to be done with a ternary, filter_var or some other method
    (there are quite a few ways to check for existence).

    I tried to look in the PHP source to see if i could make a patch to
    add this but i couldn't find the function that defines the isset
    function (i wanted to base it on that). So a pointer to the right
    location would be nice (along with docs that tell me what i all need
    to do to implement a new php function).

    https://svn.php.net/viewvc/php/php-src/trunk/Zend/zend_language_parser.y?rev
    ision=306938&view=markup

    Look for "isset_variables:", then zend_do_isset_or_isempty

    isset() lives in the parser and requires some advanced knowledge of the
    opcodes (personally I'm not there yet)
    Oh boy, i never ever did anything in the core PHP coding so i'm certainly
    not likely to be able to understand all of that. (yet)
    So, what do you think of this?
    I like the idea, it could also be called vardef() or var_default()

    @ the rest.
    The list idea is nice, but i don't really see the added value for it.. Lets
    keep it simple, oke :)
    As for that other thread: "[PHP-DEV] Implicit isset/isempty check on
    short-ternary operator"
    I don't really know much of it, but does that mean that my suggestion is
    rejected even before i made an RFC for it?

    Regards,
    Mark
  • D. Dante Lorenso at Apr 20, 2011 at 8:41 pm

    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
  • Mark at Apr 20, 2011 at 10:50 pm

    On Wednesday, April 20, 2011, D. Dante Lorenso 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
  • Arpad Ray at Apr 20, 2011 at 11:39 pm

    On Wed, Apr 20, 2011 at 11:50 PM, Mark wrote:
    On Wednesday, April 20, 2011, D. Dante Lorenso 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

    --
    PHP Internals - PHP Runtime Development Mailing List
    To unsubscribe, visit: http://www.php.net/unsub.php
    Hi,

    As Hannes said, the other thread discussing this feature (although the
    subject line may suggest otherwise) is still active, so please move
    this discussion there:
    "[PHP-DEV] Implicit isset/isempty check on short-ternary operator"

    Cheers,

    Arpad
  • Jonathan Bond-Caron at Apr 21, 2011 at 2:49 pm

    On Wed Apr 20 04:41 PM, D. Dante Lorenso wrote:

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

    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.
    That's not the reason, there's 127 / 256 opcodes.

    So far "filled", "varset", "coallesce", "ifsetor" or any operator would likely have to manipulate opcodes.

    http://php.net/~helly/ze2-ifsetor-20040901.diff.txt

    From what I understand, zeev and andi are strongly opposed to adding a new opcode. I'm sure they have their reasons.

    Maybe there's another approach where ~ E_NONE could be passed to zend_error

    ZEND_API void zend_error(int type, const char *format, ...) /* {{{ */
    {
    if(type & E_NONE)
    return;
    }

    Then modify to zend_vm_gen.php to pass E_NONE if within 'ifsetor'.

    Either way, there's a solution for it, I think the debate is over how it's implemented.

    -- Worth reading

    https://wiki.php.net/rfc/ifsetor#rejected_features

    -- Why?

    I think it simply boils down to this:
    - PHP *developers* want a function for it.

    Derick
  • Brian Moon at Apr 21, 2011 at 6:38 pm

    I proposed something similar over 5 years ago. It ain't gonna happen
    because PHP language can't support it.
    It supports it. Several functions allow you to pass in variables that
    are not set and don't throw an error. Not sure what you are talking about.

    Brian.
  • Ferenc Kovacs at Apr 21, 2011 at 7:41 pm

    On Thu, Apr 21, 2011 at 8:37 PM, Brian Moon wrote:

    I proposed something similar over 5 years ago. It ain't gonna happen
    because PHP language can't support it.
    It supports it. Several functions allow you to pass in variables that are
    not set and don't throw an error. Not sure what you are talking about.
    ?
    which one?
    I guess that you are talking about the language constructs like isset and
    empty.
    they aren't functions.

    Tyrael
  • Brian Moon at Apr 21, 2011 at 10:07 pm

    which one?
    I guess that you are talking about the language constructs like isset and
    empty.
    they aren't functions.
    settype() for one.

    Brian.
  • Ferenc Kovacs at Apr 22, 2011 at 5:32 am

    On Fri, Apr 22, 2011 at 12:07 AM, Brian Moon wrote:

    which one?
    I guess that you are talking about the language constructs like isset and
    empty.
    they aren't functions.
    settype() for one.

    Brian.
    yeah you are right, passing arguments by reference doesn't trigger the
    notice, but I'm not sure that it is applicable in our case.

    Tyrael
  • Ben Schmidt at Apr 23, 2011 at 2:04 am

    yeah you are right, passing arguments by reference doesn't trigger the
    notice, but I'm not sure that it is applicable in our case.
    Yeah, it wouldn't help. For instance, 42 or "default" can't be passed by
    reference, so you couldn't actually provide a default value to
    coalesce() if you implemented it like that, which would make it pretty
    useless. :-)

    Ben.
  • Mark at Apr 27, 2011 at 3:18 pm
    Any idea on how to progress with this idea?
    I certainly would like to see it in PHP 5.4 but i don't have the knowledge
    nor time to figure out the php internals (in C...)

    On Sat, Apr 23, 2011 at 4:04 AM, Ben Schmidt
    wrote:
    yeah you are right, passing arguments by reference doesn't trigger the
    notice, but I'm not sure that it is applicable in our case.
    Yeah, it wouldn't help. For instance, 42 or "default" can't be passed by
    reference, so you couldn't actually provide a default value to
    coalesce() if you implemented it like that, which would make it pretty
    useless. :-)

    Ben.






    --
    PHP Internals - PHP Runtime Development Mailing List
    To unsubscribe, visit: http://www.php.net/unsub.php

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupphp-internals @
categoriesphp
postedApr 20, '11 at 2:55p
activeApr 27, '11 at 3:18p
posts20
users9
websitephp.net

People

Translate

site design / logo © 2023 Grokbase