

Why doesn't this code simply return a to z as the output? - hardik988
http://stackoverflow.com/questions/4098345

======
alextgordon
It seems to me that the issue is that $i++ is nonsensical if $i is a string
(and $i _is_ a string, not a character). But instead of raising an error, PHP
soldiers on and tries to apply some completely unexpected function.

~~~
photon_off
Perl does this, too. And is it really _that_ unexpected? How many possible
things could 'z'+1 return? The only possibilities that make sense to me are
"{", "aa", or an error. I like PHP because it's flexible, and this often makes
life easier. If you're really going to try to use strings as integers, then
you deserve what's coming to you. If you're going to use this behavior as a
feature, then you've just saved yourself a chunk of time.

~~~
pbhjpbhj
>How many possible things could 'z'+1 return?

That are less than or equal to z?

~~~
eru
To z or to 'z'?

~~~
pbhjpbhj
Yes.

------
rll
Well, the choice was between having 'z'++ go to 'aa' vs. '{'. In most cases
when you are comparing strings you want to do so alphabetically, not by their
underlying character code. As mentioned in the thread there, Perl made the
same decision. If you want to compare by character code, use ord() on it.

Writing a loop like that is also a bit of a weird edge-case. You would
typically just call range('a','z') to get an array of a through z.

~~~
bdr
There is a third, correct choice: don't allow incrementing of strings.
Everything you say about _comparing_ is true, but that's a separate issue.

~~~
photon_off
Please explain: what makes this choice "correct?" Is it because it's your
personal preference?

Different languages have different features. I've been coding PHP for years,
and I've never encountered this feature before. I suspect that's true because
I don't usually treat strings like integers. However, now that I know it's
possible, I might actually use it to generate random strings or do something
useful. I don't see it as something that is incorrect, I see it as how the
language is implemented.

~~~
prodigal_erik
$x++ doesn't overflow (repeat a value you've already seen), yet produces a
value less than the prior $x. This means PHP has two different total orderings
over strings, one of which is inaccessible except by brute force. I find that
indefensibly wrong. The odds I wanted this are zero, so silently getting it
anyway is the kind of ridiculous misbehavior that makes me glad I can avoid
the language.

~~~
BrandonM
> The odds I wanted this are zero, so silently getting it anyway is the kind
> of ridiculous misbehavior that makes me glad I can avoid the language.

Not all useful values have to be order-able. If you just want to generate
dictionary keys or unique IDs, you don't care about the ordering property of
the values at all. And if you do care, all that matters is that ordering a
given set of values is consistent. This behavior satisfies that criteria.

------
gregable
That is ridiculous.

~~~
msbarnett
PHP is like that. Few other languages would feel comfortable with
simultaneously defining 'z'+1 as 'aa', while asserting that 'aa' is strictly
less than 'z'.

~~~
toolate
While I agree that 'z'++ being 'aa' is a bit silly, I can't ever see anyone
making the case that 'aa' should be greater than 'z'.

~~~
zackattack
'z'++ is _not_ 'aa'. 'z'++ is not even valid PHP. $z='z';$p=$z++; does nothing
either.

it's only in the for-loop context.

~~~
Nitramp
Not really, after $p = $z++ $p is 'z', $z is 'aa', as you'd kind-of expect.
But the difference between literals and expressions/variable references of
course just adds icing to the cake of bizareness.

------
TimothyBurgess
This actually makes perfect sense to me. Knowing this would have really come
in handy a while back when I was creating my dynamic excel spreadsheet
generator in PHP. While it doesn't follow ASCII char indexes, it follows a
base 26 numbering system.

Edit: Oh... and if anyone was wondering, PHP's "char" function (provided an
index) should perform similar to incrementing an ASCII char in C/C++.

~~~
daeken
Note: _chr_ not _char_

------
zackattack
people talk about RoR, Django, Haskell++, Node.js, you name it. me? i'm
sticking with PHP. i'm gonna be damn good at this language eventually.

~~~
mahmud
PHP can remain insane longer than you can remain alive.

