Re: [PHP] Why does it work this way?

2001-04-03 Thread Yasuo Ohgaki

I didn't read carefully yours and original  post. Sorry.

Anyway,
warn_plus_overloading=On   ; warn if the + operator is used with strings
does not raise warnings on my PHP4.0.4pl1 for some reason.

I think incrementing strings should results in 1 if type juggling works as
documented.

--
Yasuo Ohgaki


""Mark Roedel"" <[EMAIL PROTECTED]> wrote in message
[EMAIL PROTECTED]">news:[EMAIL PROTECTED]...
> -Original Message-
> From: Yasuo Ohgaki [mailto:[EMAIL PROTECTED]]
> Sent: Monday, April 02, 2001 8:00 PM
> To: [EMAIL PROTECTED]
> Subject: Re: [PHP] Why does it work this way?
>
>
> I guess PHP is doing this
>
> $str = "a";
> $str++;
>
> as
>
> $str = "a";
> $str = $str + $str; // $str now stores "aa"

On my installation of PHP (4.0.4pl1, Apache 1.3.17, FreeBSD 4.2-STABLE),
the above snippet results in $str storing a zero value.  (Which makes
sense.  The numerical value of "a" is zero.)

However

$str = "a";
$str++;

results in $str storing the value "b".

It's

$str = "z";
$str++;

that results in "aa" being stored in $str.

(This behavior was, in fact, what started the thread you replied into.)


---
Mark Roedel ([EMAIL PROTECTED])  ||  "There cannot be a crisis next week.
Systems Programmer / WebMaster  ||   My schedule is already full."
 LeTourneau University  ||-- Henry Kissinger


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]



-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]




RE: [PHP] Why does it work this way?

2001-04-03 Thread Mark Roedel

> -Original Message-
> From: Yasuo Ohgaki [mailto:[EMAIL PROTECTED]]
> Sent: Monday, April 02, 2001 8:00 PM
> To: [EMAIL PROTECTED]
> Subject: Re: [PHP] Why does it work this way?
> 
> 
> I guess PHP is doing this
> 
> $str = "a";
> $str++;
> 
> as
> 
> $str = "a";
> $str = $str + $str; // $str now stores "aa"

On my installation of PHP (4.0.4pl1, Apache 1.3.17, FreeBSD 4.2-STABLE),
the above snippet results in $str storing a zero value.  (Which makes
sense.  The numerical value of "a" is zero.)

However

$str = "a";
$str++;
 
results in $str storing the value "b".

It's

$str = "z";
$str++;

that results in "aa" being stored in $str.

(This behavior was, in fact, what started the thread you replied into.)


---
Mark Roedel ([EMAIL PROTECTED])  ||  "There cannot be a crisis next week.
Systems Programmer / WebMaster  ||   My schedule is already full."
 LeTourneau University  ||-- Henry Kissinger


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]




Re: [PHP] Why does it work this way?

2001-04-02 Thread Yasuo Ohgaki

It seems PHP used to allow "+" for string concatenation according to php.ini

warn_plus_overloading = off ; my php.ini entry

If you turn on this warring, you may see warning for that.

I guess PHP is doing this

$str = "a";
$str++;

as

$str = "a";
$str = $str + $str; // $str now stores "aa"

PHP syntax/functions looks and works like C most of the time, but there many
function/feature that does not work like C.

Regards,
--
Yasuo Ohgaki


""Mark Roedel"" <[EMAIL PROTECTED]> wrote in message
[EMAIL PROTECTED]">news:[EMAIL PROTECTED]...
-Original Message-
From: Boget, Chris [mailto:[EMAIL PROTECTED]]
Sent: Monday, April 02, 2001 11:12 AM
To: Mark Roedel
Subject: RE: [PHP] Why does it work this way?


>> Because "z"+1 turns out to be "aa" (which, if you ask me,
>> makes about as much sense as any of the alternatives),
>
> How is it that "z" + 1 is "aa"?  What is PHP using behind the
> scenes to get/generate this value?

Let me start by correcting myself on a minor technicality.

"z"+1 turns out to be 1 (because "z" has an integer value of 0).  It's
"z"++ that turns out to be "aa".

Perhaps one of the core developers who has a better understanding of the
behind-the-scenes operations of PHP can elaborate on this, but what it
basically points out to me is that "incrementing" and "adding one"
aren't always the same thing.  I assume this behavior is tied closely to
what PHP determines the type of a variable at any given time to be.

If we take that as a starting point, then what I see happening is that,
for the purposes of incrementing, there are certain ranges defined
within PHP.  0-9 is one,  obviously.  a-z appears to be another.  If,
for the purposes of the increment operator only, you treat the letters
of the alphabet as a range of integers, then the result you're seeing
makes perfect sense.  You get to your highest digit, then go back to
zero and increment the next column.

The problem, in the "for" loop that we started out discussing, is that
there doesn't seem to be any way to make a comparison that follows those
same rules.  You can't treat them as integers, because "aa" and "z" have
the same integer-value -- zero.  Treating them as strings gives the
result we've already seen.

Other interesting sort-of-related notes:

While incrementing works (sort of) on strings, decrementing doesn't
appear to have any effect at all.

$test="z";
echo $test--;
echo $test;

results in "zz"

Incrementing only appears to work on strings within particular ranges of
characters.

$test="(";
echo $test++;
echo $test;

results in "(("


---
Mark Roedel   | "The most overlooked advantage to owning a
Systems Programmer|  computer is that if they foul up there's no
LeTourneau University |  law against whacking them around a little."
Longview, Texas, USA  |  -- Owen Porterfield

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]



-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]




[PHP] Why does it work this way?

2001-04-02 Thread Mark Roedel

-Original Message-
From: Boget, Chris [mailto:[EMAIL PROTECTED]]
Sent: Monday, April 02, 2001 11:12 AM
To: Mark Roedel
Subject: RE: [PHP] Why does it work this way?


>> Because "z"+1 turns out to be "aa" (which, if you ask me, 
>> makes about as much sense as any of the alternatives), 
>
> How is it that "z" + 1 is "aa"?  What is PHP using behind the 
> scenes to get/generate this value? 

Let me start by correcting myself on a minor technicality.

"z"+1 turns out to be 1 (because "z" has an integer value of 0).  It's
"z"++ that turns out to be "aa".

Perhaps one of the core developers who has a better understanding of the
behind-the-scenes operations of PHP can elaborate on this, but what it
basically points out to me is that "incrementing" and "adding one"
aren't always the same thing.  I assume this behavior is tied closely to
what PHP determines the type of a variable at any given time to be.

If we take that as a starting point, then what I see happening is that,
for the purposes of incrementing, there are certain ranges defined
within PHP.  0-9 is one,  obviously.  a-z appears to be another.  If,
for the purposes of the increment operator only, you treat the letters
of the alphabet as a range of integers, then the result you're seeing
makes perfect sense.  You get to your highest digit, then go back to
zero and increment the next column.

The problem, in the "for" loop that we started out discussing, is that
there doesn't seem to be any way to make a comparison that follows those
same rules.  You can't treat them as integers, because "aa" and "z" have
the same integer-value -- zero.  Treating them as strings gives the
result we've already seen.

Other interesting sort-of-related notes:

While incrementing works (sort of) on strings, decrementing doesn't
appear to have any effect at all.

$test="z";
echo $test--;
echo $test;

results in "zz"

Incrementing only appears to work on strings within particular ranges of
characters.

$test="(";
echo $test++;
echo $test;

results in "(("


---
Mark Roedel   | "The most overlooked advantage to owning a
Systems Programmer|  computer is that if they foul up there's no
LeTourneau University |  law against whacking them around a little."
Longview, Texas, USA  |  -- Owen Porterfield 

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]




[PHP] Why does it work this way?

2001-04-02 Thread Boget, Chris

for( $i = "a"; $i <= "z"; $i++ ) {
}

Not entirely too sure why, but the above works.  And
it's really cool that it does. :)
However, it doesn't work as one would expect (aside 
from the fact that it works at all :p).  Just looking at 
it, I would think that it would iterate from "a" to "z"
and stop.  But in fact, what it's doing is going from 
"a" to "yz", having iterated all of the alphabet using
double letters ( "aa", "az", "bb", "bz" ... "yy", "yz").
Why is that?

Just trying to understand. :)

Thanks for any help/insight you can provide!

Chris