# Combined assignment operator for short ternary

- Thread: https://externals.io/message/62228
- Original archive: https://news-web.php.net/php.internals/62228
- Started by Sebastian Krebs on 2012-08-17T21:41:56+00:00
- Messages: 18
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #62228 — Sebastian Krebs — 2012-08-17T21:41:56+00:00

URL: https://externals.io/message/62228#62228

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

---

## #62230 — keisial@gmail.com — 2012-08-17T22:26:08+00:00

URL: https://externals.io/message/62228#62230 · In reply to: #62228

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

---

## #62231 — Tjerk Meesters — 2012-08-18T02:36:41+00:00

URL: https://externals.io/message/62228#62231 · In reply to: #62228

Sent from my iPhone



> 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

---

## #62249 — Andrew Faulds — 2012-08-18T22:20:06+00:00

URL: https://externals.io/message/62228#62249 · In reply to: #62231

>
> Sent from my iPhone
>
> 
>
> > […]
> 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.
> > […]


-- 
Andrew Faulds
http://ajf.me/

---

## #62259 — Sebastian Krebs — 2012-08-19T14:58:11+00:00

URL: https://externals.io/message/62228#62259 · In reply to: #62249

Am 19.08.2012 00:20, schrieb Andrew Faulds:
> 
> > […]
> 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.

> > […]

---

## #62459 — Sebastian Krebs — 2012-08-24T21:01:42+00:00

URL: https://externals.io/message/62228#62459 · In reply to: #62228

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

---

## #62460 — Ferenc Kovacs — 2012-08-24T21:11:59+00:00

URL: https://externals.io/message/62228#62460 · In reply to: #62228

On Fri, Aug 17, 2012 at 11:41 PM, Sebastian Krebs <krebs.seb@gmail.com>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

---

## #62464 — Stas Malyshev — 2012-08-24T23:00:32+00:00

URL: https://externals.io/message/62228#62464 · In reply to: #62459

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

---

## #62465 — Sebastian Krebs — 2012-08-24T23:43:18+00:00

URL: https://externals.io/message/62228#62465 · In reply to: #62460

Am 24.08.2012 23:11, schrieb Ferenc Kovacs:
>
>
> On Fri, Aug 17, 2012 at 11:41 PM, Sebastian Krebs <krebs.seb@gmail.com
> <mailto:krebs.seb@gmail.com>> 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

---

## #62466 — Sebastian Krebs — 2012-08-24T23:49:13+00:00

URL: https://externals.io/message/62228#62466 · In reply to: #62464

Am 25.08.2012 01:00, schrieb Stas Malyshev:
> Hi!
>
> > […]
>
> 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

---

## #62467 — Nikita Popov — 2012-08-24T23:53:48+00:00

URL: https://externals.io/message/62228#62467 · In reply to: #62466

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

---

## #62488 — Ferenc Kovacs — 2012-08-25T16:38:19+00:00

URL: https://externals.io/message/62228#62488 · In reply to: #62465

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

---

## #62493 — Sebastian Krebs — 2012-08-25T17:49:35+00:00

URL: https://externals.io/message/62228#62493 · In reply to: #62488

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

---

## #62495 — Ferenc Kovacs — 2012-08-25T18:09:43+00:00

URL: https://externals.io/message/62228#62495 · In reply to: #62493

2012.08.25. 19:50, "Sebastian Krebs" <krebs.seb@gmail.com> ezt írta:
>
> Am 25.08.2012 18:38, schrieb Ferenc Kovacs:
>
> > […]
the
> > […]
>
>
> Only works on the green field ;) And/or if you are alone ...
>
>
> > […]
>
>
> Same here :p
>
>
> > […]
>
>
>
> --
> 
> 
>

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.

---

## #62496 — Sebastian Krebs — 2012-08-25T18:24:59+00:00

URL: https://externals.io/message/62228#62496 · In reply to: #62467

Am 25.08.2012 01:53, schrieb Nikita Popov:
> 
> > […]
>
> 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

---

## #62502 — David Muir — 2012-08-25T23:30:48+00:00

URL: https://externals.io/message/62228#62502 · In reply to: #62495

> 2012.08.25. 19:50, "Sebastian Krebs" <krebs.seb@gmail.com> ezt írta:
> > […]
> the
> > […]
> 
> 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

---

## #62503 — Ferenc Kovacs — 2012-08-25T23:57:05+00:00

URL: https://externals.io/message/62228#62503 · In reply to: #62502

>
>
> > […]
>
> 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

---

## #62504 — Sebastian Krebs — 2012-08-26T00:19:03+00:00

URL: https://externals.io/message/62228#62504 · In reply to: #62503

Am 26.08.2012 01:57, schrieb Ferenc Kovacs:
>
> > […]
>     optional
> > […]
>
>     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
