# Feature Proposal: Allow letter decrementing

- Thread: https://externals.io/message/68160
- Original archive: https://news-web.php.net/php.internals/68160
- Started by Chris London on 2013-07-18T13:46:14+00:00
- Messages: 22
- 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

---

## #68160 — Chris London — 2013-07-18T13:46:14+00:00

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

My friend shared some code with me today that wasn't working for him. He
was incrementing letters like this:

$letter = 'A';
echo ++$letter; // Output: B

He was then trying to decrement letters like this:

$letter = 'B';
echo --$letter; // Output: B

He was really confused why his code wasn't working.  Obviously the answer
is, PHP doesn't support decrementing letters.

My question is, why specifically doesn't it?

It seems logical that if we support increment we should support decrement.

My proposal:

1) Support decrementing letters
- or -
2) Throw a notice explaining that decrementing is not supported so
developers quickly know they can't do that

One hiccup. I imagine maybe the reason we don't support decrementing is
because what would happen if you tried to decrement 'A'. In that case I
would suggest either returning `NULL` or throwing a notice.

Thanks for the input

Chris London

---

## #68161 — Mateusz Kocielski — 2013-07-18T14:20:58+00:00

URL: https://externals.io/message/68160#68161 · In reply to: #68160

> It seems logical that if we support increment we should support decrement.
>
> My proposal:
>
> 1) Support decrementing letters
> - or -
> 2) Throw a notice explaining that decrementing is not supported so
> developers quickly know they can't do that
>
> One hiccup. I imagine maybe the reason we don't support decrementing is
> because what would happen if you tried to decrement 'A'. In that case I
> would suggest either returning `NULL` or throwing a notice.
>
> Thanks for the input
>
> Chris London

Hello,

 Does anybody understand the rules behind the string incrementation? What's
the explanation of following behaviour?

 '$x="ZZ";$x++;var_dump($x);' returns string(3) "AAA"
 '$x=" ZZ";$x++;var_dump($x);' returns string(3) " AA"

Regards,
Mateusz Kocielski

---

## #68162 — johannes@schlueters.de — 2013-07-18T14:33:40+00:00

URL: https://externals.io/message/68160#68162 · In reply to: #68160

> My friend shared some code with me today that wasn't working for him. He
> was incrementing letters like this:
> 
> $letter = 'A';
> echo ++$letter; // Output: B
> 
> He was then trying to decrement letters like this:
> 
> $letter = 'B';
> echo --$letter; // Output: B
> 
> He was really confused why his code wasn't working.  Obviously the answer
> is, PHP doesn't support decrementing letters.

Oviously this documented in
http://de2.php.net/manual/en/language.operators.increment.php ;-)

> My question is, why specifically doesn't it?
> 
> It seems logical that if we support increment we should support decrement.

++$string; will always succeed. But what should $s = 'a'; --$s; result
in?

johannes

---

## #68163 — Xinchen Hui — 2013-07-18T14:37:49+00:00

URL: https://externals.io/message/68160#68163 · In reply to: #68161

在 2013-7-18，22:31，Mateusz Kocielski <shm@digitalsun.pl> 写道：

It seems logical that if we support increment we should support decrement.


My proposal:


1) Support decrementing letters

- or -

2) Throw a notice explaining that decrementing is not supported so

developers quickly know they can't do that


One hiccup. I imagine maybe the reason we don't support decrementing is

because what would happen if you tried to decrement 'A'. In that case I

would suggest either returning `NULL` or throwing a notice.


Thanks for the input


Chris London


Hello,

Does anybody understand the rules behind the string incrementation? What's
the explanation of following behaviour?

'$x="ZZ";$x++;var_dump($x);' returns string(3) "AAA"
'$x=" ZZ";$x++;var_dump($x);' returns string(3) " AA"

see http://www.php.net/manual/en/language.operators.increment.php

thanks

Regards,
Mateusz Kocielski

---

## #68165 — Martin Amps — 2013-07-18T19:08:56+00:00

URL: https://externals.io/message/68160#68165 · In reply to: #68163

> One hiccup. I imagine maybe the reason we don't support decrementing is
> because what would happen if you tried to decrement 'A'. In that case I
> would suggest either returning `NULL` or throwing a notice.

It would make sense to make it stay as ‘A’ like incrementing ‘Z' stays as ‘Z’.

I do agree that string manipulation with arithmetic operators needs work/clarification.



> 在 2013-7-18，22:31，Mateusz Kocielski <shm@digitalsun.pl> 写道：
> 
> It seems logical that if we support increment we should support decrement.
> 
> 
> My proposal:
> 
> 
> 1) Support decrementing letters
> 
> - or -
> 
> 2) Throw a notice explaining that decrementing is not supported so
> 
> developers quickly know they can't do that
> 
> 
> One hiccup. I imagine maybe the reason we don't support decrementing is
> 
> because what would happen if you tried to decrement 'A'. In that case I
> 
> would suggest either returning `NULL` or throwing a notice.
> 
> 
> Thanks for the input
> 
> 
> Chris London
> 
> 
> Hello,
> 
> Does anybody understand the rules behind the string incrementation? What's
> the explanation of following behaviour?
> 
> '$x="ZZ";$x++;var_dump($x);' returns string(3) "AAA"
> '$x=" ZZ";$x++;var_dump($x);' returns string(3) " AA"
> 
> see http://www.php.net/manual/en/language.operators.increment.php
> 
> thanks
> 
> Regards,
> Mateusz Kocielski

---

## #68166 — Chris London — 2013-07-18T19:10:10+00:00

URL: https://externals.io/message/68160#68166 · In reply to: #68165

Something I learned today is incrementing 'Z' goes to 'AA' but that helps
the point that it could use more work/clarification




> One hiccup. I imagine maybe the reason we don't support decrementing is
> because what would happen if you tried to decrement 'A'. In that case I
> would suggest either returning `NULL` or throwing a notice.
>
>
> It would make sense to make it stay as ‘A’ like incrementing ‘Z' stays as
> ‘Z’.
>
> I do agree that string manipulation with arithmetic operators needs
> work/clarification.
>
> 
>
> 在 2013-7-18，22:31，Mateusz Kocielski <shm@digitalsun.pl> 写道：
>
> It seems logical that if we support increment we should support decrement.
>
>
> My proposal:
>
>
> 1) Support decrementing letters
>
> - or -
>
> 2) Throw a notice explaining that decrementing is not supported so
>
> developers quickly know they can't do that
>
>
> One hiccup. I imagine maybe the reason we don't support decrementing is
>
> because what would happen if you tried to decrement 'A'. In that case I
>
> would suggest either returning `NULL` or throwing a notice.
>
>
> Thanks for the input
>
>
> Chris London
>
>
> Hello,
>
> Does anybody understand the rules behind the string incrementation? What's
> the explanation of following behaviour?
>
> '$x="ZZ";$x++;var_dump($x);' returns string(3) "AAA"
> '$x=" ZZ";$x++;var_dump($x);' returns string(3) " AA"
>
> see http://www.php.net/manual/en/language.operators.increment.php
>
> thanks
>
> Regards,
> Mateusz Kocielski

---

## #68167 — Chris London — 2013-07-18T19:22:49+00:00

URL: https://externals.io/message/68160#68167

The one caveat about decrementing "A" and it goes to `NULL` is that if you
increment `NULL` it goes to 1 so that may be confusing.  We could also
decrement "A" and it goes to `FALSE` and FALSE++ is still `FALSE` 


On Thu, Jul 18, 2013 at 1:10 PM, Phil Sturgeon <email@philsturgeon.co.uk>wrote:

>  Incrementing "Z" results in "AA", because we're starting again.
> Decrementing "A" should probably go to `NULL` as -A is no good.
>
> --
> Phil Sturgeon
>
> 
>
> One hiccup. I imagine maybe the reason we don't support decrementing is
> because what would happen if you tried to decrement 'A'. In that case I
> would suggest either returning `NULL` or throwing a notice.
>
>
> It would make sense to make it stay as ‘A’ like incrementing ‘Z' stays as
> ‘Z’.
>
> I do agree that string manipulation with arithmetic operators needs
> work/clarification.
>
> 
>
> 在 2013-7-18，22:31，Mateusz Kocielski <shm@digitalsun.pl> 写道：
>
> It seems logical that if we support increment we should support decrement.
>
>
> My proposal:
>
>
> 1) Support decrementing letters
>
> - or -
>
> 2) Throw a notice explaining that decrementing is not supported so
>
> developers quickly know they can't do that
>
>
> One hiccup. I imagine maybe the reason we don't support decrementing is
>
> because what would happen if you tried to decrement 'A'. In that case I
>
> would suggest either returning `NULL` or throwing a notice.
>
>
> Thanks for the input
>
>
> Chris London
>
>
> Hello,
>
> Does anybody understand the rules behind the string incrementation? What's
> the explanation of following behaviour?
>
> '$x="ZZ";$x++;var_dump($x);' returns string(3) "AAA"
> '$x=" ZZ";$x++;var_dump($x);' returns string(3) " AA"
>
> see http://www.php.net/manual/en/language.operators.increment.php
>
> thanks
>
> Regards,
> Mateusz Kocielski

---

## #68176 — Sara Golemon — 2013-07-18T23:11:01+00:00

URL: https://externals.io/message/68160#68176 · In reply to: #68160

> My question is, why specifically doesn't it?
>
> Because Perl.

---

## #68177 — Chris London — 2013-07-18T23:18:09+00:00

URL: https://externals.io/message/68160#68177 · In reply to: #68176

I hear Perl 6 supports Str decrementing


> > […]

---

## #68181 — Sherif Ramadan — 2013-07-19T05:37:25+00:00

URL: https://externals.io/message/68160#68181 · In reply to: #68177

> I hear Perl 6 supports Str decrementing
>
>
I heard of Perl 6 once.

---

## #68182 — Mateusz Kocielski — 2013-07-19T06:39:45+00:00

URL: https://externals.io/message/68160#68182 · In reply to: #68163

> > […]
>
> see http://www.php.net/manual/en/language.operators.increment.php

I don't see any explanation for the examples above. Documentation says:

[...] For example, in PHP and Perl $a = 'Z'; $a++; turns $a into 'AA' [...]

I'd expect " ZZ" to become " AAA" rather than " AA". I'm not sure if
it's a bug in documentation, in code. Probably nobody can even
distinguish if it's a bug or feature.

Mateusz Kocielski

---

## #68183 — Yasuo Ohgaki — 2013-07-19T07:08:11+00:00

URL: https://externals.io/message/68160#68183 · In reply to: #68182

Hi,

2013/7/19 Mateusz Kocielski <shm@digitalsun.pl>

> > […]
>
> I don't see any explanation for the examples above. Documentation says:
>
> [...] For example, in PHP and Perl $a = 'Z'; $a++; turns $a into 'AA' [...]
>
> I'd expect " ZZ" to become " AAA" rather than " AA". I'm not sure if
> it's a bug in documentation, in code. Probably nobody can even
> distinguish if it's a bug or feature.
>

You may play with one liner.

php -r "\$a = ' ZZ';for (\$i=0; \$i<100000000;\$i++) var_dump(++\$a);"

This feature existed when I start using PHP3.
Manual says

> Note that character variables can be incremented but not decremented and
> even so only plain ASCII characters (a-z and A-Z) are supported.
Incrementing/decrementing
> other character variables has no effect, the original string is unchanged.

This behavior is due to the algorithm used for string ++/--.
I think it looks at the end of char and tries to increment, but it
finds non [a-zA-Z] char and stops operation. Therefore, it cycles
over and over.

It may seems strange, but I don't see reason to change.

Regards,

--
Yasuo Ohgaki
yohgaki@ohgaki.net

---

## #68184 — Peter Lind — 2013-07-19T07:34:28+00:00

URL: https://externals.io/message/68160#68184 · In reply to: #68183

Interesting to note that although Perl 6 is apparently capable of
decrementing strings, it doesn't fully mirror the incrementing:

http://feather.perl6.nl/syn/S03.html#line_516

Specifically: decrementing 'AAA' would not turn into 'ZZ' but would error,
according to that link

-- 
<hype>
WWW: plphp.dk / plind.dk
CV: careers.stackoverflow.com/peterlind
LinkedIn: plind
Twitter: kafe15
</hype

---

## #68187 — Yasuo Ohgaki — 2013-07-19T08:59:34+00:00

URL: https://externals.io/message/68160#68187 · In reply to: #68184

Hi,

I take a look at the code.
It does support [0-9] also.

[yohgaki@dev tests]$ php -r "\$a = ' a10';for (\$i=0; \$i<10;\$i++)
var_dump(++\$a);"
string(4) " a11"
string(4) " a12"
string(4) " a13"
string(4) " a14"
string(4) " a15"
string(4) " a16"
string(4) " a17"
string(4) " a18"
string(4) " a19"
string(4) " a20"

It sounds good to have string decrements.


2013/7/19 Peter Lind <peter.e.lind@gmail.com>

> Interesting to note that although Perl 6 is apparently capable of
> decrementing strings, it doesn't fully mirror the incrementing:
>
> http://feather.perl6.nl/syn/S03.html#line_516
>
> Specifically: decrementing 'AAA' would not turn into 'ZZ' but would error,
> according to that link
>

Carry handle of decrements is more complex than increments.
It may be the reason.

Regards,

--
Yasuo Ohgaki
yohgaki@ohgaki.net

---

## #68188 — Dan Cryer — 2013-07-19T09:18:39+00:00

URL: https://externals.io/message/68160#68188 · In reply to: #68187

What's the intended use case for string increment / decrement?

It just seems like madness to me, using mathematical operators with
strings, producing seemingly arbitrary results in some circumstances (C ->
B -> A -> `NULL` / False ?).

Also what happens in other languages? Take for example German, in which ß
comes after S, Ü after U, and so on.




> Hi,
>
> I take a look at the code.
> It does support [0-9] also.
>
> [yohgaki@dev tests]$ php -r "\$a = ' a10';for (\$i=0; \$i<10;\$i++)
> var_dump(++\$a);"
> string(4) " a11"
> string(4) " a12"
> string(4) " a13"
> string(4) " a14"
> string(4) " a15"
> string(4) " a16"
> string(4) " a17"
> string(4) " a18"
> string(4) " a19"
> string(4) " a20"
>
> It sounds good to have string decrements.
>
>
> 2013/7/19 Peter Lind <peter.e.lind@gmail.com>
>
> > […]
> error,
> > […]
>
> Carry handle of decrements is more complex than increments.
> It may be the reason.
>
> Regards,
>
> --
> Yasuo Ohgaki
> yohgaki@ohgaki.net
>



-- 

*Dan Cryer*

 07590 698944
 dan@dancryer.com
 +Dan <https://plus.google.com/101400775372325517263>
 @dancryer <http://www.twitter.com/dancryer

---

## #68190 — Xinchen Hui — 2013-07-19T09:30:29+00:00

URL: https://externals.io/message/68160#68190 · In reply to: #68188

> What's the intended use case for string increment / decrement?
>
+ 1

could we please stop wasting time on such issues?  many thanks


> It just seems like madness to me, using mathematical operators with
> strings, producing seemingly arbitrary results in some circumstances (C ->
> B -> A -> `NULL` / False ?).
>
> Also what happens in other languages? Take for example German, in which ß
> comes after S, Ü after U, and so on.
>
>
> 
>
> > […]
>
>
>
> --
>
> *Dan Cryer*
>
>  07590 698944
>  dan@dancryer.com
>  +Dan <https://plus.google.com/101400775372325517263>
>  @dancryer <http://www.twitter.com/dancryer>
>



-- 
惠新宸        laruence
Senior PHP Engineer
http://www.laruence.com

---

## #68191 — Peter Lind — 2013-07-19T09:31:15+00:00

URL: https://externals.io/message/68160#68191 · In reply to: #68188

> What's the intended use case for string increment / decrement?
>
>
Personally, I instantly think of mirroring spreadsheet columns - works
quite well in that context.


> It just seems like madness to me, using mathematical operators with
> strings, producing seemingly arbitrary results in some circumstances (C ->
> B -> A -> `NULL` / False ?).
>
>
Throw a warning and don't decrement A/a any further - that's not arbitrary.


> Also what happens in other languages? Take for example German, in which ß
> comes after S, Ü after U, and so on.
>
>
Nothing, works purely on ascii, as is currently the case. Perl handles
other character sets though - see the link I posted for details on how.

Regards
Peter

-- 
<hype>
WWW: plphp.dk / plind.dk
CV: careers.stackoverflow.com/peterlind
LinkedIn: plind
Twitter: kafe15
</hype

---

## #68192 — Yasuo Ohgaki — 2013-07-19T10:34:40+00:00

URL: https://externals.io/message/68160#68192 · In reply to: #68191

Hi,

2013/7/19 Peter Lind <peter.e.lind@gmail.com>

> 
>
> > […]
> Personally, I instantly think of mirroring spreadsheet columns - works
> quite well in that context.
>

++/-- 'XYZ00001234' would have use cases.


>
>
> > […]
> Throw a warning and don't decrement A/a any further - that's not arbitrary.
>

-- is more problematic.
--'XYZ0000' would be 'XYZ0000' or 'XYY00000' or 'XYY'?

It would be better not to change length of string, IMO.
e.g. --'XYZ0000' became 'XYY0000'

All chars would stop decrement at lowest chars of [0-9], [a-z], [A-Z].
e.g. Lowest value of 'XYZ0000' is 'AAA0000'.

This is not a symmetric operation of ++, but it's impossible to achieve
symmetric
operation ++/-- on strings anyway.


>
> > […]
> Nothing, works purely on ascii, as is currently the case. Perl handles
> other character sets though - see the link I posted for details on how.
>

There may be need for this, but it will be very complex code than now.
[0-9a-zA-Z] would be enough. PHP doesn't have default multibyte char
module, too.

Regards,

--
Yasuo Ohgaki
yohgaki@ohgaki.net

---

## #68193 — Nikita Popov — 2013-07-19T10:39:02+00:00

URL: https://externals.io/message/68160#68193 · In reply to: #68160

> My friend shared some code with me today that wasn't working for him. He
> was incrementing letters like this:
>
> $letter = 'A';
> echo ++$letter; // Output: B
>
> He was then trying to decrement letters like this:
>
> $letter = 'B';
> echo --$letter; // Output: B
>
> He was really confused why his code wasn't working.  Obviously the answer
> is, PHP doesn't support decrementing letters.
>
> My question is, why specifically doesn't it?
>
> It seems logical that if we support increment we should support decrement.
>
> My proposal:
>
> 1) Support decrementing letters
> - or -
> 2) Throw a notice explaining that decrementing is not supported so
> developers quickly know they can't do that
>
> One hiccup. I imagine maybe the reason we don't support decrementing is
> because what would happen if you tried to decrement 'A'. In that case I
> would suggest either returning `NULL` or throwing a notice.
>
> Thanks for the input
>
> Chris London
>

I don't think we should add string decrementing due to the rather complex
logic behind it (imho the string incrementing that we have shouldn't be
there either). What we *should* add though is a notice or warning in case
an increment/decrement operation fails. incdec is one of the few places
where we currently silently ignore FAILUREs, instead of throwing some
helpful error message :)

Nikita

---

## #68195 — Peter Lind — 2013-07-19T10:46:22+00:00

URL: https://externals.io/message/68160#68195 · In reply to: #68192

> Hi,
>
> 2013/7/19 Peter Lind <peter.e.lind@gmail.com>
>
> > […]
>
> ++/-- 'XYZ00001234' would have use cases.
>
>
> > […]
>
> -- is more problematic.
> --'XYZ0000' would be 'XYZ0000' or 'XYY00000' or 'XYY'?
>
>
php -r '$a = "YZ9"; var_dump(++$a);'
gives me ZA0

If  php -r '$a = "ZA0"; var_dump(--$a);' doesn't give YZ9 you'll just be
left scratching your head.

Granted, you might be left scratching your head as to why string
incrementing works at all, but that can of worms was opened long ago.

It would be better not to change length of string, IMO.
> e.g. --'XYZ0000' became 'XYY0000'
>
>
I'd go with --XYZ0000 -> XYY9999
Also, the string length shouldn't factor into it.


> All chars would stop decrement at lowest chars of [0-9], [a-z], [A-Z].
> e.g. Lowest value of 'XYZ0000' is 'AAA0000'.
>
> This is not a symmetric operation of ++, but it's impossible to achieve
> symmetric
> operation ++/-- on strings anyway.
>
>
Only at the edge case - and I personally can't see why that would mean
asymmetric operation in the rest of the cases.


-- 
<hype>
WWW: plphp.dk / plind.dk
CV: careers.stackoverflow.com/peterlind
LinkedIn: plind
Twitter: kafe15
</hype

---

## #68205 — Ralf Lang — 2013-07-19T16:14:56+00:00

URL: https://externals.io/message/68160#68205 · In reply to: #68195

> > […]

I once had an app which needed to generate long continuous ranges of
alpha codes. But even then I did not implement -- in PHP userland.

I think if there is a need for --, each application would need its own
"correct" behaviour. AAA-- may be AA or ZZ or ZZZ or ZZ0 or ZZ9 or even
0ZZ depending on use case.

If there is a reason to do this in C rather than PHP userland code, it's
probably best fit in an extension.


-- 
Ralf Lang
Linux Consultant / Developer
Tel.: +49-170-6381563
Mail: lang@b1-systems.de
B1 Systems GmbH
Osterfeldstraße 7 / 85088 Vohburg / http://www.b1-systems.de
GF: Ralph Dehner / Unternehmenssitz: Vohburg / AG: Ingolstadt,HRB 3537

---

## #68223 — Yasuo Ohgaki — 2013-07-19T20:45:23+00:00

URL: https://externals.io/message/68160#68223 · In reply to: #68193

Hi Nikita,

2013/7/19 Nikita Popov <nikita.ppv@gmail.com>

> I don't think we should add string decrementing due to the rather complex
> logic behind it (imho the string incrementing that we have shouldn't be
> there either).
>

Right. It is impossible to symmetric operation ++/-- for strings.
Not implementing --'STRING' is a valid option.


> What we *should* add though is a notice or warning in case
> an increment/decrement operation fails. incdec is one of the few places
> where we currently silently ignore FAILUREs, instead of throwing some
> helpful error message :)
>

Making --'STRING'  raises `E_NOTICE` is reasonable option.
If PHP don't support, then it would better to notice users.
It may be useful to spot bugs, too. Raising error might be more
helpful for many users.

I don't have concrete idea for --'STRING'. `E_NOTICE` might be the
way to go.

Regards,

--
Yasuo Ohgaki
yohgaki@ohgaki.net
