Implied cast differs from explicit cast [message #176657] |
Mon, 16 January 2012 01:10 |
lb
Messages: 4 Registered: January 2012
Karma:
|
Junior Member |
|
|
When converting some strings to int (or double), I am finding that
an implied cast get different results from explicit cast. This happens
when the string contains a hex representation. The implied cast converts
the hex value, but the explicit cast returns 0. The manual under "String
conversion to numbers" says nothing about it allowing hex values. This is
with PHP-5.3.9 and older.
Script:
<?php
$s1 = '0x12';
$t1 = $s1 + 0;
echo "Implied cast t1=$t1\n";
$t2 = (int)$s1 + 0;
echo "Explicit cast t2=$t2\n";
Output:
Implied cast t1=18
Explicit cast t2=0
I think this is odd. A side problem is that is_numeric('0x12') is true, but
((int)'0x12') is 0. Does anyone think this a bug?
|
|
|