String -> Number Automatic Conversion

------_=_NextPart_001_01C50A0A.5E66A2A4
Content-Type: text/plain

I am not sure if this is a bug or I am just doing something dumb but I am having a problem with automatic converting of strings to a number between 0 and -1. For example:

$p1 = Array ( 0 => "-.15",
1 => "-.15" + 0,
2 => (float) "-.15" + 0,
5 => "-1.15",
6 => "-1.15" + 0,
7 => (float) "-1.15" + 0,
8 => ".15",
9 => ".15" + 0,
10 => (float) ".15" + 0,
11 => "1.15",
12 => "1.5" + 0,
13 => (float) "1.5" + 0
);


If I print $p1 I get:

Array
(
[0] => -.15
[1] => 0
[2] => -0.15
[5] => -1.15
[6] => -1.15
[7] => -1.15
[8] => .15
[9] => 0.15
[10] => 0.15
[11] => 1.15
[12] => 1.5
[13] => 1.5
)


Note that the results for element [1] is 0 not -.15 as I had expected. It does not matter what value is added to the string. Any value outside the range of -1 < value < 0 gives the correct results.

Is this a bug or am I missing something.


Thanks for any help,


Art Nuzzo

------_=_NextPart_001_01C50A0A.5E66A2A4--
Nuzzo Art-CINT116 [ Do, 03 Februar 2005 17:06 ] [ ID #624150 ]

Re: String -> Number Automatic Conversion

Nuzzo Art-CINT116 wrote:
> 1 => "-.15" + 0,
> 6 => "-1.15" + 0,
> [1] => 0
> [6] => -1.15

> Is this a bug or am I missing something.

Looks like a bug to me...

It's remotely possible that it's documented somewhere that "-.15" doesn't
turn into a number, but that would be pretty bogus, eh?

But who knows: There may well be a technical or philosophical reason for
not allowing that format. I can't see why, but I ain't the guy writing
the code :-)

Check and file it at http://bugs.php.net

I suppose it's also remotely possible that "rounding errors" are at fault,
but that's pretty damn big round error... More likely, the regex to
detect a "number" at the start of a string doesn't include "-.[0-9]*" as a
valid case.

Possible work-around:
I suspect that
$p1[14] = "-0.15" + 0;
will do what you want, since the leading 0 will probably trigger the regex
properly.

Try it and see.

PS You should probably have posted the PHP version and whatnot as well, as
I wouldn't be surprised if the bug is version-specific...

--
Like Music?
http://l-i-e.com/artists.htm

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Richard Lynch [ Do, 03 Februar 2005 17:41 ] [ ID #624152 ]

RE: String -> Number Automatic Conversion

>
> Possible work-around:
> I suspect that
> $p1[14] = "-0.15" + 0;
> will do what you want, since the leading 0 will probably
> trigger the regex properly.
>
> Try it and see.

Changing it to -0.15 + 0 works. This data is actually coming from a numeric field in an Oracle database using the OCI interface so I am not sure why it's a string instead of a number but that's a separate issue I need to look into.


>
> PS You should probably have posted the PHP version and
> whatnot as well, as I wouldn't be surprised if the bug is
> version-specific...
>
>

Ah... Yes, I meant to include that information:

HP-UX: 11.11
PHP: 4.3.9


Art Nuzzo

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Nuzzo Art-CINT116 [ Do, 03 Februar 2005 18:36 ] [ ID #624322 ]

RE: String -> Number Automatic Conversion

Nuzzo Art-CINT116 wrote:
>> Possible work-around:
>> I suspect that
>> $p1[14] = "-0.15" + 0;
>> will do what you want, since the leading 0 will probably
>> trigger the regex properly.
>>
>> Try it and see.
>
> Changing it to -0.15 + 0 works. This data is actually coming from a
> numeric field in an Oracle database using the OCI interface so I am not
> sure why it's a string instead of a number but that's a separate issue I
> need to look into.

I'm not certain about OCI or others, but generally PHP database extensions
retrieve all data as text...

At least, the last time I checked on that, that's what it was...

Could be wrong or out of date.

Rarely worry about data type in PHP, once you understand the type juggling
and conversion rules.

--
Like Music?
http://l-i-e.com/artists.htm

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Richard Lynch [ Do, 03 Februar 2005 19:07 ] [ ID #624330 ]

RE: String -> Number Automatic Conversion

On 03 February 2005 16:07, Nuzzo Art-CINT116 wrote:

> I am not sure if this is a bug or I am just doing something
> dumb but I am having a problem with automatic converting of
> strings to a number between 0 and -1. For example:

> 1 => "-.15" + 0,

> [1] => 0

> Is this a bug or am I missing something.

It's a bug -- see http://bugs.php.net/bug.php?id=30726

Reported fixed on 18-Jan-2005, so I guess you're looking at 4.3.11/5.0.4 to
actually contain the fix.

Cheers!

Mike

------------------------------------------------------------ ---------
Mike Ford, Electronic Information Services Adviser,
Learning Support Services, Learning & Information Services,
JG125, James Graham Building, Leeds Metropolitan University,
Headingley Campus, LEEDS, LS6 3QS, United Kingdom
Email: m.ford [at] leedsmet.ac.uk
Tel: +44 113 283 2600 extn 4730 Fax: +44 113 283 3211


To view the terms under which this email is distributed, please go to http://disclaimer.leedsmet.ac.uk/email.htm

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
M.Ford [ Do, 03 Februar 2005 19:53 ] [ ID #624338 ]

RE: String -> Number Automatic Conversion

>
> > Is this a bug or am I missing something.
>
> It's a bug -- see http://bugs.php.net/bug.php?id=30726
>
> Reported fixed on 18-Jan-2005, so I guess you're looking at
> 4.3.11/5.0.4 to actually contain the fix.
>
> Cheers!
>
> Mike
>
> ------------------------------------------------------------ ---------
> Mike Ford, Electronic Information Services Adviser,
> Learning Support Services, Learning & Information Services,
> JG125, James Graham Building, Leeds Metropolitan University,
> Headingley Campus, LEEDS, LS6 3QS, United Kingdom
> Email: m.ford [at] leedsmet.ac.uk
> Tel: +44 113 283 2600 extn 4730 Fax: +44 113 283 3211
>


Are there patches available that I can apply to my existing PHP (4.3.9) or is my only alternate is to put a work around in place until 4.3.11 is released?

Do you know when 4.3.11 might be released?



Thanks,



Art Nuzzo

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Nuzzo Art-CINT116 [ Do, 03 Februar 2005 20:40 ] [ ID #624342 ]

RE: String -> Number Automatic Conversion

On 03 February 2005 19:40, Nuzzo Art-CINT116 wrote:

> > > Is this a bug or am I missing something.
> >
> > It's a bug -- see http://bugs.php.net/bug.php?id=30726
> >
> > Reported fixed on 18-Jan-2005, so I guess you're looking at
> > 4.3.11/5.0.4 to actually contain the fix.
> >
>
> Are there patches available that I can apply to my existing
> PHP (4.3.9) or is my only alternate is to put a work around
> in place until 4.3.11 is released?

You could always download the latest development snapshot from
http://snaps.php.net/, as referenced in the bug report mentioned above.

Cheers!

Mike

------------------------------------------------------------ ---------
Mike Ford, Electronic Information Services Adviser,
Learning Support Services, Learning & Information Services,
JG125, James Graham Building, Leeds Metropolitan University,
Headingley Campus, LEEDS, LS6 3QS, United Kingdom
Email: m.ford [at] leedsmet.ac.uk
Tel: +44 113 283 2600 extn 4730 Fax: +44 113 283 3211


To view the terms under which this email is distributed, please go to http://disclaimer.leedsmet.ac.uk/email.htm

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
M.Ford [ Fr, 04 Februar 2005 11:02 ] [ ID #625715 ]
PHP » gmane.comp.php.general » String -> Number Automatic Conversion

Vorheriges Thema: Credit card storing, for processing
Nächstes Thema: Headers already sent error