Re: Integer casts broken or...?

From: Date: Tue, 15 May 2001 03:59:14 +0000
Subject: Re: Integer casts broken or...?
References: 1 2 3 4  Groups: php.dev 
Request: Send a blank email to php-dev+get-55291@lists.php.net to get a copy of this message
Brian Moon writes: > I know it would confuse me to have 0009 turned into an octal or hex if I > type cast it to (int). When I think of (int), I only think of their > ultimate decimal value. Perhaps there needs to be a new type cast ((hex)? > (oct)?) that will interpret variables in their hex or octal value. I know > it is still a long integer in value, but it is a different representation of > that number. That's along the lines of what I was thinking--another cast (and perhaps an optional arg to intval()?) which would respect the base--maybe not separate (oct) or (hex)--or maybe so--or something like (intbase) which just respected the base (since octal and hex are the only ones strtol() claims to handle anyway). I admit that name is clumsy at best. :) As it stands is_numeric('0x24') returns true but intval('0x24') returns 0--which seems to conflict. Changing the existing cast would probably surprise a lot of people though :). > As for why it was that way to begin with, I am not sure. I could be that it > would be too confusing like you say or that type casting was introduced > before using hex or octal numbers was. Ah, the world is so full of unanswered questions. :) > Brian Moon > ---------------------------------------------------------------------- > dealnews.com, Inc. > Makers of dealnews, dealmac > http://dealnews.com/ | > http://dealmac.com/ > > > ----- Original Message ----- > From: "Lars Torben Wilson" <torben@php.net> > To: "Brian Moon" <brianm@dealnews.com> > Cc: <php-dev@lists.php.net> > Sent: Monday, May 14, 2001 4:55 PM > Subject: Re: [PHP-DEV] Integer casts broken or...? > > > > Brian Moon writes: > > > This is correct. The type casting converts the string into its integer > > > value. If there is a non-numeric character in the string, it stops at > that > > > point. So "09" is 9 and "09t" is 9 but "0t9" is 0. > > > > > > Brian Moon > > > > Yeah, I know--it winds up being a strtol() call. However, since PHP > > does interpret octal and hex integer literals, it seems odd that it > > won't interpret them when encapsulated in strings. This does look > > intentional though, since strtol() is called with a base of 10 in > > zend_operators.c (line 259 in cvs); replacing it with a base of 0 > > gives the result I (personally) would have expected. > > > > So--is it intentional because it would screw some people up to have > > left-zero-padded string numbers interpreted as octals, or is there > > another deeper reason that using 0 as the base for strtol() is a bad > > idea, or is this unintentional? > > > > > > > ----- Original Message ----- > > > From: "Lars Torben Wilson" <torben@php.net> > > > To: <php-dev@lists.php.net> > > > Sent: Monday, May 14, 2001 1:29 PM > > > Subject: [PHP-DEV] Integer casts broken or...? > > > > > > > > > > > > > > Type casting to int from string works only for decimal values--is this > > > > by design? Example: > > > > > > > > echo (int) '09' . ', ' . (int) '0x24'; > > > > > > > > ...produces: > > > > > > > > 9, 0 > > > > > > > > which seems intuitively wrong (I'd have expected 0, 36). > > > > > > > > Is this wrong, or should I just document it? > > > > > > > > > > > > Thanks, > > > > > > > > Torben > > > > > > > > -- > > > > Torben Wilson <torben@php.net> > > > > http://www.thebuttlesschaps.com > > > > +1.604.709.0506 > > > > > > > > > > > > -- > > > > PHP Development Mailing List > > > > <http://www.php.net/> > > > > To unsubscribe, e-mail: php-dev-unsubscribe@lists.php.net > > > > For additional commands, e-mail: php-dev-help@lists.php.net > > > > To contact the list administrators, e-mail: > php-list-admin@lists.php.net > > > > > > > > > > > > > > > > > > > -- > > Torben Wilson <torben@php.net> > > http://www.thebuttlesschaps.com > > +1.604.709.0506 > > > > > > > > > -- > PHP Development Mailing List <http://www.php.net/> > To unsubscribe, e-mail: php-dev-unsubscribe@lists.php.net > For additional commands, e-mail: php-dev-help@lists.php.net > To contact the list administrators, e-mail: php-list-admin@lists.php.net > -- Torben Wilson <torben@php.net> http://www.thebuttlesschaps.com +1.604.709.0506

« previous php.dev (#55291) next »