# Proposed RFC

- Thread: https://externals.io/message/96719
- Original archive: https://news-web.php.net/php.internals/96719
- Started by Antony D'Andrea on 2016-11-03T15:02:08+00:00
- Messages: 6
- 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

---

## #96719 — Antony D'Andrea — 2016-11-03T15:02:08+00:00

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

Hi all,

First off, this is my first time e-mailing internals or even thinking about submitting RFC. Please forgive me if I fail to follow some kind of convention.

In PHP 7.0, we were given the Null Coalesce operator. For example:

       echo $array['key']??"key is not set"

would be the same as:

      echo (isset($array['key'])?$array['key']:"key is not set"

This is a great feature, that makes code much cleaner.

This works on the principle that "$array['key']" is "NULL".

I would like to propose a new feature that is as clean as this but is a slightly different use case. This would require a new operator (up for discussion, but an early idea is "?!") For example:

    echo (!is_infinite($n1/$n2)?!0);

Would output ($n1/$n2) if it is "true" and 0 if false.

Right now, the closest we have to this is ?: operator. The problem with this is that it could get very messy as you still have to do:

    echo (!is_infinite($n1/$n2)?$n1/$n2:0);

I have obviously over simplified the example. You wouldn't have a big problem in this case, but if the subject of the function is much longer, it can become complicated very quickly.

Alternatively, a perhaps more general feature would be to just have the same functionality as the Null Coalesce, but with true/false rather than Null/Not Null.

Please let me know if there is something in these ideas or anyway to improve them. I should also note that I would need a volunteer to implement this as my "C" skills are non-existent and I wouldn't have the confidence to delve into the the PHP source.

Thanks for your time.

Best,

Antony D'Andrea

---

## #96721 — David Rodrigues — 2016-11-03T15:48:18+00:00

URL: https://externals.io/message/96719#96721 · In reply to: #96719

You have tried the ?: operator?

    echo !is_infinite($n1/$n2) ?: 0;

It should returns true or 0, for this case.

2016-11-03 13:02 GMT-02:00 Antony D'Andrea <contactme@antonydandrea.com>:
> Hi all,
>
> First off, this is my first time e-mailing internals or even thinking about submitting RFC. Please forgive me if I fail to follow some kind of convention.
>
> In PHP 7.0, we were given the Null Coalesce operator. For example:
>
>        echo $array['key']??"key is not set"
>
> would be the same as:
>
>       echo (isset($array['key'])?$array['key']:"key is not set"
>
> This is a great feature, that makes code much cleaner.
>
> This works on the principle that "$array['key']" is "NULL".
>
> I would like to propose a new feature that is as clean as this but is a slightly different use case. This would require a new operator (up for discussion, but an early idea is "?!") For example:
>
>     echo (!is_infinite($n1/$n2)?!0);
>
> Would output ($n1/$n2) if it is "true" and 0 if false.
>
> Right now, the closest we have to this is ?: operator. The problem with this is that it could get very messy as you still have to do:
>
>     echo (!is_infinite($n1/$n2)?$n1/$n2:0);
>
> I have obviously over simplified the example. You wouldn't have a big problem in this case, but if the subject of the function is much longer, it can become complicated very quickly.
>
> Alternatively, a perhaps more general feature would be to just have the same functionality as the Null Coalesce, but with true/false rather than Null/Not Null.
>
> Please let me know if there is something in these ideas or anyway to improve them. I should also note that I would need a volunteer to implement this as my "C" skills are non-existent and I wouldn't have the confidence to delve into the the PHP source.
>
> Thanks for your time.
>
> Best,
>
> Antony D'Andrea



-- 
David Rodrigues

---

## #96722 — Bishop Bettini — 2016-11-03T16:02:49+00:00

URL: https://externals.io/message/96719#96722 · In reply to: #96719

On Thu, Nov 3, 2016 at 11:02 AM, Antony D'Andrea <
contactme@antonydandrea.com> wrote:

>
> I would like to propose a new feature that is as clean as this but is a
> slightly different use case. This would require a new operator (up for
> discussion, but an early idea is "?!") For example:
>
>     echo (!is_infinite($n1/$n2)?!0);
>
> Would output ($n1/$n2) if it is "true" and 0 if false.
>

Welcome!

Let me read back what I'm hearing. If the predicate returns truthy, you
want to return the argument to the predicate, otherwise the indicated
default?

How would this be used outside predicates, such as on string functions?

Consider: echo strpos('abc', 'b') ?! 0;

What would this output, and what would the purpose of such a construct be?

---

## #96723 — David Rodrigues — 2016-11-03T16:03:40+00:00

URL: https://externals.io/message/96719#96723

> Yes I have but I would like it to return ($n1/$n2) or whatever this input is, without repeating it again.

But it should do exactly that, except for a falsy return (eg. zero,
empty, null, false) - note INFINITY is a truth value, so 10/0, for
instance, will return `INF` instead of "else value".
For instance:

$n1 = 10;
$n2 = 5;

echo ( $n1 / $n2 ) ?: 'ELSE VALUE'; // Will returns 2

$n1 = 0;
$n2 = 5;

echo "\n";
echo ( $n1 / $n2 ) ?: 'ELSE VALUE'; // Will returns ELSE VALUE


2016-11-03 13:56 GMT-02:00 Antony D'Andrea <contactme@antonydandrea.com>:
> Hi,
>
> Yes I have but I would like it to return ($n1/$n2) or whatever this input is, without repeating it again.
> ________________________________________
> From: David Rodrigues [david.proweb@gmail.com]
> Sent: 03 November 2016 15:48
> To: Antony D'Andrea
> Cc: internals@lists.php.net
> Subject: Re: [PHP-DEV] Proposed RFC
>
> You have tried the ?: operator?
>
>     echo !is_infinite($n1/$n2) ?: 0;
>
> It should returns true or 0, for this case.
>
> 2016-11-03 13:02 GMT-02:00 Antony D'Andrea <contactme@antonydandrea.com>:
> > […]
>
>
>
> --
> David Rodrigues



-- 
David Rodrigues

---

## #96725 — Antony D'Andrea — 2016-11-03T16:11:56+00:00

URL: https://externals.io/message/96719#96725

Thanks everyone for your responses.

It has not taken long to find situations where this idea starts to fall apart. For example, in the case of strpos, I would hope for the return value, but in other cases I would want the the original input if the output is not false.

That makes this feature pretty unfeasible I think!

---

## #96906 — Stanislav Malyshev — 2016-11-16T06:17:42+00:00

URL: https://externals.io/message/96719#96906 · In reply to: #96725

Hi!

> It has not taken long to find situations where this idea starts to
> fall apart. For example, in the case of strpos, I would hope for the
> return value, but in other cases I would want the the original input
> if the output is not false.

A side note - when discussing an idea, it would be great to use a
descriptive subject next time. E.g.: "RFC idea - new ?! operator". It is
not uncommon that several RFC are discussed at the same time, much
easier when each one has a descriptive subject. Also, emails are
archived and somebody interested in the same idea in the future may try
to find this discussion a year or two from now, and the descriptive
subject would help a lot.

Thanks,
-- 
Stas Malyshev
smalyshev@gmail.com
