[UPHPU] Understanding php's logic

Mac Newbold mac at macnewbold.com
Wed Apr 7 09:14:52 MDT 2004


Today at 7:08am, David Smith said:

>I think it makes perfect sense. In C, you cannot do "Z"+1, but you can
>do 'Z'+1 (single quotes), which interprets the Z as its ASCII value.
>There is no C comparison for "Z"+1, since "Z" is a string (char* in C).
>I guess if you did this in C, you would actually be doing pointer
>arithemtic:
>
>#include <stdio>
>char *letter = "A";
>int i = 0;
>for( i=0; letter <= 'Z', letter++ )
>    printf( "%s\n", letter );
>
>And that's likely not the intended behavior. :)
>
>PHP has simply decided to handle string and the + operator that way. I
>like it, though I would never use it in a real application as I think it
>is error prone and difficult to read. :-)

If I'm understanding you right, it sounds like you're saying that PHP is
interpreting "string"++ as pointer arithmetic. While that makes perfect
sense in C, in PHP it makes no sense at all to me, especially since
"A"++=="B", "B"++=="C", etc. The one that I can't fathom or explain is how
"Z"++=="AA". That can't be pointer arithmetic...

I actually checked about a character literal (like C's 'A' syntax) in PHP
but it turns out there are no character literals, only strings.

While there are a thousand workarounds, the fact that this "mostly" works,
and the strange ways in which it doesn't, are still bugging me.

Mac

--
Mac Newbold		MNE - Mac Newbold Enterprises, LLC
mac at macnewbold.com	http://www.macnewbold.com/



More information about the UPHPU mailing list