FAQ
Hi,

Don't know, how complicated this is (and also someone (not me) must
implement it, because I can't :X), but to be in sync with the operators
the short ternary operator should be usable in conjunction with the
assignment like the other binary operators. Don't know, if anybody
understands me :D So here is an example

// instead of
$foo = $foo ?: 'default';
// Just
$foo ?:= 'default';

I have many of this "default assigments" and yes: This is just syntactic
sugar.

Regards,
Sebastian

Search Discussions

  • Ángel González at Aug 17, 2012 at 10:27 pm

    On 17/08/12 23:41, Sebastian Krebs wrote:
    Hi,

    Don't know, how complicated this is (and also someone (not me) must
    implement it, because I can't :X), but to be in sync with the
    operators the short ternary operator should be usable in conjunction
    with the assignment like the other binary operators. Don't know, if
    anybody understands me :D So here is an example

    // instead of
    $foo = $foo ?: 'default';
    // Just
    $foo ?:= 'default';

    I have many of this "default assigments" and yes: This is just
    syntactic sugar.
    This seems to enter into ASCII art land.

    $emoticon ?:=( 8)
  • Tjerk Meesters at Aug 18, 2012 at 2:36 am
    Sent from my iPhone
    On 18 Aug, 2012, at 5:41 AM, Sebastian Krebs wrote:

    Hi,

    Don't know, how complicated this is (and also someone (not me) must implement it, because I can't :X), but to be in sync with the operators the short ternary operator should be usable in conjunction with the assignment like the other binary operators. Don't know, if anybody understands me :D So here is an example

    // instead of
    $foo = $foo ?: 'default';
    // Just
    $foo ?:= 'default';
    Why not just: $foo ?= 'default';
    I have many of this "default assigments" and yes: This is just syntactic sugar.

    Regards,
    Sebastian

    --
    PHP Internals - PHP Runtime Development Mailing List
    To unsubscribe, visit: http://www.php.net/unsub.php
  • Andrew Faulds at Aug 18, 2012 at 10:20 pm

    On 18/08/12 03:36, Tjerk Meesters wrote:
    Sent from my iPhone
    On 18 Aug, 2012, at 5:41 AM, Sebastian Krebs wrote:

    Hi,

    Don't know, how complicated this is (and also someone (not me) must implement it, because I can't :X), but to be in sync with the operators the short ternary operator should be usable in conjunction with the assignment like the other binary operators. Don't know, if anybody understands me :D So here is an example

    // instead of
    $foo = $foo ?: 'default';
    // Just
    $foo ?:= 'default';
    Why not just: $foo ?= 'default';
    Just noting, I made a ML email earlier about this, except it would be
    shorthand for isset($lval) ? $lval : $default. And this has been brought
    up before several times.
    I have many of this "default assigments" and yes: This is just syntactic sugar.

    Regards,
    Sebastian

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

    --
    Andrew Faulds
    http://ajf.me/
  • Sebastian Krebs at Aug 19, 2012 at 2:58 pm

    Am 19.08.2012 00:20, schrieb Andrew Faulds:
    On 18/08/12 03:36, Tjerk Meesters wrote:

    Sent from my iPhone
    On 18 Aug, 2012, at 5:41 AM, Sebastian Krebs wrote:

    Hi,

    Don't know, how complicated this is (and also someone (not me) must
    implement it, because I can't :X), but to be in sync with the
    operators the short ternary operator should be usable in conjunction
    with the assignment like the other binary operators. Don't know, if
    anybody understands me :D So here is an example

    // instead of
    $foo = $foo ?: 'default';
    // Just
    $foo ?:= 'default';
    Why not just: $foo ?= 'default';
    Just noting, I made a ML email earlier about this, except it would be
    shorthand for isset($lval) ? $lval : $default. And this has been brought
    up before several times.
    The "except" is important: I don't want to change the behaviour in any way.
    I have many of this "default assigments" and yes: This is just
    syntactic sugar.

    Regards,
    Sebastian

    --
    PHP Internals - PHP Runtime Development Mailing List
    To unsubscribe, visit: http://www.php.net/unsub.php
  • Sebastian Krebs at Aug 24, 2012 at 9:01 pm
    Hi,

    Just want to push my thread ^^

    Nobody interested? Too stupid? I need to learn C (:X)? Too complicated?
    To much impact (on something)? Too less sense? BC? Anything not
    mentioned here?

    To point that out: I _don't_ want to change the behaviour, which means
    $foo['xy'] ?:= 'bar';
    would trigger a notice like
    $foo['xy'] = $foo['xy'] ?: 'bar';
    would do.

    Regards,
    Sebastian

    Am 17.08.2012 23:41, schrieb Sebastian Krebs:
    Hi,

    Don't know, how complicated this is (and also someone (not me) must
    implement it, because I can't :X), but to be in sync with the operators
    the short ternary operator should be usable in conjunction with the
    assignment like the other binary operators. Don't know, if anybody
    understands me :D So here is an example

    // instead of
    $foo = $foo ?: 'default';
    // Just
    $foo ?:= 'default';

    I have many of this "default assigments" and yes: This is just syntactic
    sugar.

    Regards,
    Sebastian
  • Stas Malyshev at Aug 24, 2012 at 11:00 pm
    Hi!
    To point that out: I _don't_ want to change the behaviour, which means
    $foo['xy'] ?:= 'bar';
    would trigger a notice like
    $foo['xy'] = $foo['xy'] ?: 'bar';
    Then I personally don't see much point in it. Saving a couple of
    keystrokes IMHO is not really worth it.
    --
    Stanislav Malyshev, Software Architect
    SugarCRM: http://www.sugarcrm.com/
    (408)454-6900 ext. 227
  • Sebastian Krebs at Aug 24, 2012 at 11:49 pm

    Am 25.08.2012 01:00, schrieb Stas Malyshev:
    Hi!
    To point that out: I _don't_ want to change the behaviour, which means
    $foo['xy'] ?:= 'bar';
    would trigger a notice like
    $foo['xy'] = $foo['xy'] ?: 'bar';
    Then I personally don't see much point in it. Saving a couple of
    keystrokes IMHO is not really worth it.
    Hi,

    It's like with any other compound operator: A _real_ reason isn't there.
    But saying "It's not worth it" is something I can live with (even if I
    don't know how much effort it would take ;)). I just asked myself: '?:'
    is (a kind of?) binary operator and every other binary operator is
    available as compound operator, so why not '?:', so I don't have to
    repeat the variable?

    Regards,
    Sebastian
  • Nikita Popov at Aug 24, 2012 at 11:53 pm

    On Sat, Aug 25, 2012 at 1:49 AM, Sebastian Krebs wrote:
    Hi,

    It's like with any other compound operator: A _real_ reason isn't there. But
    saying "It's not worth it" is something I can live with (even if I don't
    know how much effort it would take ;)). I just asked myself: '?:' is (a kind
    of?) binary operator and every other binary operator is available as
    compound operator, so why not '?:', so I don't have to repeat the variable?
    No, every other binary operator is not available. If I interpret this
    right one could say that all short-circuiting operators are not
    available. E.g. you also can't write $foo &&= $bar; or $foo ||= $bar.
    The ?: operator also falls in this category of short-circuiting
    logical operators.
  • Sebastian Krebs at Aug 25, 2012 at 6:25 pm

    Am 25.08.2012 01:53, schrieb Nikita Popov:
    On Sat, Aug 25, 2012 at 1:49 AM, Sebastian Krebs wrote:
    Hi,

    It's like with any other compound operator: A _real_ reason isn't there. But
    saying "It's not worth it" is something I can live with (even if I don't
    know how much effort it would take ;)). I just asked myself: '?:' is (a kind
    of?) binary operator and every other binary operator is available as
    compound operator, so why not '?:', so I don't have to repeat the variable?
    No, every other binary operator is not available. If I interpret this
    right one could say that all short-circuiting operators are not
    available. E.g. you also can't write $foo &&= $bar; or $foo ||= $bar.
    The ?: operator also falls in this category of short-circuiting
    logical operators.

    Yes, makes sense... And thinking further I think myself it's better to
    keep it as it is. So thanks for your time :)

    Regards,
    Sebastian
  • Ferenc Kovacs at Aug 24, 2012 at 9:12 pm

    On Fri, Aug 17, 2012 at 11:41 PM, Sebastian Krebs wrote:

    Hi,

    Don't know, how complicated this is (and also someone (not me) must
    implement it, because I can't :X), but to be in sync with the operators the
    short ternary operator should be usable in conjunction with the assignment
    like the other binary operators. Don't know, if anybody understands me :D
    So here is an example

    // instead of
    $foo = $foo ?: 'default';
    // Just
    $foo ?:= 'default';
    would this trigger a notice if $foo is not defined?
    if yes, then it would be different from the current behavior of the ternary
    operator.
    if no, then I would never use this.
    I mean if I have to set the variable before the check, then I would put the
    check into the assignment.

    --
    Ferenc Kovács
    @Tyr43l - http://tyrael.hu
  • Sebastian Krebs at Aug 24, 2012 at 11:43 pm

    Am 24.08.2012 23:11, schrieb Ferenc Kovacs:

    On Fri, Aug 17, 2012 at 11:41 PM, Sebastian Krebs wrote:

    Hi,

    Don't know, how complicated this is (and also someone (not me) must
    implement it, because I can't :X), but to be in sync with the
    operators the short ternary operator should be usable in conjunction
    with the assignment like the other binary operators. Don't know, if
    anybody understands me :D So here is an example

    // instead of
    $foo = $foo ?: 'default';
    // Just
    $foo ?:= 'default';


    would this trigger a notice if $foo is not defined?
    if yes, then it would be different from the current behavior of the
    ternary operator.
    Couldn't believe it, thus I tested it myself


    $ php -a
    Interactive mode enabled

    <?php
    $f = $f ?: 'bar';
    PHP Notice: Undefined variable: f in - on line 2
    PHP Stack trace:
    PHP 1. {main}() -:0

    Notice: Undefined variable: f in - on line 2

    Call Stack:
    8.9779 619512 1. {main}() -:0


    Don't know, what you are talking about, but the notice _is_ the current
    behaiour and therefore: No difference.

    if no, then I would never use this.
    I mean if I have to set the variable before the check, then I would put
    the check into the assignment.
    The main thought about it was

    function foo ($bar = null) {
    $bar = $bar ?: 'default';
    }


    I _always_ use 'null' as default

    - If you want to omit a parameter, but want to set one after that, you
    don't need to look whats the default: It's 'null'
    - Ive often enough seen something like

    function foo ($limit = 10) { /* code */ }
    // Somewhere else
    function bar ($limit = 50) { /* code */ $foo($limit); /* code */}
    // Even somewhere else
    bar();

    Imagine there are some other methods and functions in between it's quite
    annoying.


    --
    Ferenc Kovács
    @Tyr43l - http://tyrael.hu
  • Ferenc Kovacs at Aug 25, 2012 at 4:38 pm

    would this trigger a notice if $foo is not defined?
    if yes, then it would be different from the current behavior of the
    ternary operator.
    Couldn't believe it, thus I tested it myself
    snip

    Don't know, what you are talking about, but the notice _is_ the current
    behaiour and therefore: No difference.

    Sorry, I messed up that email. What I wanted to say:
    If it accepts unset variable, then I could see usecases for it, but then it
    would behave differently than the current ternary.
    If it doesn't accept unset variable then it would in line with what we
    have, but I don't see any usecase for it, because I would have to set it
    before checking that it is falsely or not, in which case I would set it to
    the default if not set already.

    if no, then I would never use this.
    I mean if I have to set the variable before the check, then I would put
    the check into the assignment.
    The main thought about it was

    function foo ($bar = null) {
    $bar = $bar ?: 'default';
    }
    If you wanted to enforce the 'default' value to be the default if no
    argument passed, then you could use $bar = 'default' in the method
    signature.
    So I guess that you use that construct to handle when the argument is
    passed, but it contains a falsely value ("0", array(), 0, etc.).
    I agree that this can be useful in some cases.

    I _always_ use 'null' as default
    For me, it isn't always null, sometimes it is boolean true/false, or an
    empty array.

    - If you want to omit a parameter, but want to set one after that, you
    don't need to look whats the default: It's 'null'
    my IDE takes care of that problem for me.

    - Ive often enough seen something like

    function foo ($limit = 10) { /* code */ }
    // Somewhere else
    function bar ($limit = 50) { /* code */ $foo($limit); /* code */}
    // Even somewhere else
    bar();
    same here.

    --
    Ferenc Kovács
    @Tyr43l - http://tyrael.hu
  • Sebastian Krebs at Aug 25, 2012 at 5:49 pm
    Am 25.08.2012 18:38, schrieb Ferenc Kovacs:
    would this trigger a notice if $foo is not defined?
    if yes, then it would be different from the current behavior of the
    ternary operator.


    Couldn't believe it, thus I tested it myself


    snip

    Don't know, what you are talking about, but the notice _is_ the
    current behaiour and therefore: No difference.


    Sorry, I messed up that email. What I wanted to say:
    If it accepts unset variable, then I could see usecases for it, but then
    it would behave differently than the current ternary.
    If it doesn't accept unset variable then it would in line with what we
    have, but I don't see any usecase for it, because I would have to set it
    before checking that it is falsely or not, in which case I would set it
    to the default if not set already.



    if no, then I would never use this.
    I mean if I have to set the variable before the check, then I
    would put
    the check into the assignment.


    The main thought about it was

    function foo ($bar = null) {
    $bar = $bar ?: 'default';
    }


    If you wanted to enforce the 'default' value to be the default if no
    argument passed, then you could use $bar = 'default' in the method
    signature.
    So I guess that you use that construct to handle when the argument is
    passed, but it contains a falsely value ("0", array(), 0, etc.).
    I agree that this can be useful in some cases.


    I _always_ use 'null' as default


    For me, it isn't always null, sometimes it is boolean true/false, or an
    empty array.


    - If you want to omit a parameter, but want to set one after that,
    you don't need to look whats the default: It's 'null'


    my IDE takes care of that problem for me.
    Only works on the green field ;) And/or if you are alone ...
    - Ive often enough seen something like

    function foo ($limit = 10) { /* code */ }
    // Somewhere else
    function bar ($limit = 50) { /* code */ $foo($limit); /* code */}
    // Even somewhere else
    bar();


    same here.
    Same here :p
    --
    Ferenc Kovács
    @Tyr43l - http://tyrael.hu
  • Ferenc Kovacs at Aug 25, 2012 at 6:09 pm
    2012.08.25. 19:50, "Sebastian Krebs" <krebs.seb@gmail.com> ezt írta:
    Am 25.08.2012 18:38, schrieb Ferenc Kovacs:
    would this trigger a notice if $foo is not defined?
    if yes, then it would be different from the current behavior of
    the
    ternary operator.


    Couldn't believe it, thus I tested it myself


    snip

    Don't know, what you are talking about, but the notice _is_ the
    current behaiour and therefore: No difference.


    Sorry, I messed up that email. What I wanted to say:
    If it accepts unset variable, then I could see usecases for it, but then
    it would behave differently than the current ternary.
    If it doesn't accept unset variable then it would in line with what we
    have, but I don't see any usecase for it, because I would have to set it
    before checking that it is falsely or not, in which case I would set it
    to the default if not set already.



    if no, then I would never use this.
    I mean if I have to set the variable before the check, then I
    would put
    the check into the assignment.


    The main thought about it was

    function foo ($bar = null) {
    $bar = $bar ?: 'default';
    }


    If you wanted to enforce the 'default' value to be the default if no
    argument passed, then you could use $bar = 'default' in the method
    signature.
    So I guess that you use that construct to handle when the argument is
    passed, but it contains a falsely value ("0", array(), 0, etc.).
    I agree that this can be useful in some cases.


    I _always_ use 'null' as default


    For me, it isn't always null, sometimes it is boolean true/false, or an
    empty array.


    - If you want to omit a parameter, but want to set one after that,
    you don't need to look whats the default: It's 'null'


    my IDE takes care of that problem for me.

    Only works on the green field ;) And/or if you are alone ...

    - Ive often enough seen something like

    function foo ($limit = 10) { /* code */ }
    // Somewhere else
    function bar ($limit = 50) { /* code */ $foo($limit); /* code */}
    // Even somewhere else
    bar();


    same here.

    Same here :p

    --
    Ferenc Kovács
    @Tyr43l - http://tyrael.hu


    --
    PHP Internals - PHP Runtime Development Mailing List
    To unsubscribe, visit: http://www.php.net/unsub.php
    So you are saying that your (teams) IDE doesn't tell you the method
    signature which contains also the default values?
    I guess that the fact that many of the php core functions have optional
    arguments and non null defaults must be really a PITA for you.
  • David Muir at Aug 25, 2012 at 11:31 pm

    On 26/08/2012, at 4:09 AM, Ferenc Kovacs wrote:

    2012.08.25. 19:50, "Sebastian Krebs" <krebs.seb@gmail.com> ezt írta:
    Am 25.08.2012 18:38, schrieb Ferenc Kovacs:
    would this trigger a notice if $foo is not defined?
    if yes, then it would be different from the current behavior of
    the
    ternary operator.


    Couldn't believe it, thus I tested it myself


    snip

    Don't know, what you are talking about, but the notice _is_ the
    current behaiour and therefore: No difference.


    Sorry, I messed up that email. What I wanted to say:
    If it accepts unset variable, then I could see usecases for it, but then
    it would behave differently than the current ternary.
    If it doesn't accept unset variable then it would in line with what we
    have, but I don't see any usecase for it, because I would have to set it
    before checking that it is falsely or not, in which case I would set it
    to the default if not set already.



    if no, then I would never use this.
    I mean if I have to set the variable before the check, then I
    would put
    the check into the assignment.


    The main thought about it was

    function foo ($bar = null) {
    $bar = $bar ?: 'default';
    }


    If you wanted to enforce the 'default' value to be the default if no
    argument passed, then you could use $bar = 'default' in the method
    signature.
    So I guess that you use that construct to handle when the argument is
    passed, but it contains a falsely value ("0", array(), 0, etc.).
    I agree that this can be useful in some cases.


    I _always_ use 'null' as default


    For me, it isn't always null, sometimes it is boolean true/false, or an
    empty array.


    - If you want to omit a parameter, but want to set one after that,
    you don't need to look whats the default: It's 'null'


    my IDE takes care of that problem for me.

    Only works on the green field ;) And/or if you are alone ...

    - Ive often enough seen something like

    function foo ($limit = 10) { /* code */ }
    // Somewhere else
    function bar ($limit = 50) { /* code */ $foo($limit); /* code */}
    // Even somewhere else
    bar();


    same here.

    Same here :p

    --
    Ferenc Kovács
    @Tyr43l - http://tyrael.hu


    --
    PHP Internals - PHP Runtime Development Mailing List
    To unsubscribe, visit: http://www.php.net/unsub.php
    So you are saying that your (teams) IDE doesn't tell you the method
    signature which contains also the default values?
    I guess that the fact that many of the php core functions have optional
    arguments and non null defaults must be really a PITA for you.
    I think what he was alluding to is the problem where one changes the default. All usages of the method/function then need to be updated. Using null today is very much like using the proposed 'default' parameter skipping RFC.

    Cheers,
    David
  • Ferenc Kovacs at Aug 25, 2012 at 11:57 pm

    So you are saying that your (teams) IDE doesn't tell you the method
    signature which contains also the default values?
    I guess that the fact that many of the php core functions have optional
    arguments and non null defaults must be really a PITA for you.
    I think what he was alluding to is the problem where one changes the
    default.
    All usages of the method/function then need to be updated. Using null today
    is very much like using the proposed 'default' parameter skipping RFC.
    true, using null as a default value for your optional arguments means that
    you don't really need the parameter skipping RFC (at least for the
    functions that you control) or the named params, because you can pass null
    for the in-between optional arguments.
    ofc. most modern IDEs also have some kind of refactoring feature, which
    would make it easier for you to update the function calls to reflect the
    change in the default value, but it would be harder to tell the original
    intention ($limit is 30 here, because it was the default and the caller
    wanted to set the next optional argument, or $limit is intentionally set to
    30 and the next optional argument also happen to be needed).
    thanks for clearing that one up for me.

    Having every optional argument defaulting to null means that it can be
    frequent scenario, when you have a variable, which is guaranteed to be
    defined, but can be null.
    But I still think that it isn't that frequent that you want to keep the non
    falsely values or set a default value otherwise.
    I mean if you expect a number, then you don't want your passed 0 to be
    replaced with the default, or if you expect a string, you don't want "0" or
    maybe even "" to be replaced with the default, etc..
    So you couldn't use the ternary assignment for those scenarios.

    --
    Ferenc Kovács
    @Tyr43l - http://tyrael.hu
  • Sebastian Krebs at Aug 26, 2012 at 12:19 am

    Am 26.08.2012 01:57, schrieb Ferenc Kovacs:
    So you are saying that your (teams) IDE doesn't tell you the method
    signature which contains also the default values?
    I guess that the fact that many of the php core functions have optional
    arguments and non null defaults must be really a PITA for you.
    I think what he was alluding to is the problem where one changes the
    default.

    All usages of the method/function then need to be updated. Using
    null today is very much like using the proposed 'default' parameter
    skipping RFC.


    true, using null as a default value for your optional arguments means
    that you don't really need the parameter skipping RFC (at least for the
    functions that you control) or the named params, because you can pass
    null for the in-between optional arguments.
    ofc. most modern IDEs also have some kind of refactoring feature, which
    would make it easier for you to update the function calls to reflect the
    change in the default value, but it would be harder to tell the original
    intention ($limit is 30 here, because it was the default and the caller
    wanted to set the next optional argument, or $limit is intentionally set
    to 30 and the next optional argument also happen to be needed).
    thanks for clearing that one up for me.

    Having every optional argument defaulting to null means that it can be
    frequent scenario, when you have a variable, which is guaranteed to be
    defined, but can be null.
    But I still think that it isn't that frequent that you want to keep the
    non falsely values or set a default value otherwise.
    I mean if you expect a number, then you don't want your passed 0 to be
    replaced with the default, or if you expect a string, you don't want "0"
    or maybe even "" to be replaced with the default, etc..
    So you couldn't use the ternary assignment for those scenarios.
    Yes, it doesn't work in this cases, but I must say

    - I try to avoid optional parameters whereever useful. The caller should
    be aware of what he is doing. I prefer to define "preferred values" as
    constants instead, that can be used whereever a preferred behaviour is
    wanted.
    - I do't implement very much functions/methods, where (e.g.) 0 is
    allowed and should _not_ be the default (same for strings and so on),
    thus '$index = $index ?: 0;' is valid regardless wether $index is 0 or
    null in the first place. Or if I expect a string the empty string '' is
    most of the time a special cases (or required). If I expect an object is
    just obvious, that they can never evaluate to false.

    I don't think we need argue about this, because this two points are more
    or less personal preferences/opinions. But this said there are only some
    few edge cases (for me), where I need to distinguish between '', 0,
    null, ..., and in this few cases, I can still use the good old `$index =
    is_null($index) ? 123 : $index;` ;).

    At the end: It works just great to use null as general-purpose-default.
    I don't care about a special 'default'-keyword, because in my mind
    something like that already exists in form of 'null': It's either "the
    default primitive value", or "no-object" (in case an object was
    expected, but not required).

    --
    Ferenc Kovács
    @Tyr43l - http://tyrael.hu

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupphp-internals @
categoriesphp
postedAug 17, '12 at 9:42p
activeAug 26, '12 at 12:19a
posts18
users8
websitephp.net

People

Translate

site design / logo © 2022 Grokbase