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:
- Support decrementing letters
- or -
- 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
It seems logical that if we support increment we should support decrement.
My proposal:
- Support decrementing letters
- or -
- Throw a notice explaining that decrementing is not supported so
developers quickly know they can't do thatOne 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 returningNULL
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
在 2013-7-18,22:31,Mateusz Kocielski shm@digitalsun.pl 写道:
It seems logical that if we support increment we should support decrement.
My proposal:
- Support decrementing letters
- or -
- 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
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 returningNULL
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:
- Support decrementing letters
- or -
- 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
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 returningNULL
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:
- Support decrementing letters
- or -
- 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
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
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
Hi,
2013/7/19 Mateusz Kocielski shm@digitalsun.pl
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.
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
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
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
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
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 linkCarry 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
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.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 linkCarry 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
--
惠新宸 laruence
Senior PHP Engineer
http://www.laruence.com
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
Hi,
2013/7/19 Peter Lind peter.e.lind@gmail.com
What's the intended use case for string increment / decrement?
Personally, I instantly think of mirroring spreadsheet columns - works
quite well in that context.
++/-- 'XYZ00001234' would have use cases.
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.
-- 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.
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.
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
Hi,
2013/7/19 Peter Lind peter.e.lind@gmail.com
What's the intended use case for string increment / decrement?
Personally, I instantly think of mirroring spreadsheet columns - works
quite well in that context.++/-- 'XYZ00001234' would have use cases.
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.-- 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
What's the intended use case for string increment / decrement?
Personally, I instantly think of mirroring spreadsheet columns - works
quite well in that context.++/-- 'XYZ00001234' would have use cases.
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
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: BHe was then trying to decrement letters like this:
$letter = 'B';
echo --$letter; // Output: BHe 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
My question is, why specifically doesn't it?
Because Perl.
I hear Perl 6 supports Str decrementing
My question is, why specifically doesn't it?
Because Perl.
I hear Perl 6 supports Str decrementing
I heard of Perl 6 once.
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: BHe was then trying to decrement letters like this:
$letter = 'B';
echo --$letter; // Output: BHe 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:
- Support decrementing letters
- or -
- Throw a notice explaining that decrementing is not supported so
developers quickly know they can't do thatOne 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 returningNULL
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
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