Bug #61701 [Opn->Nab]: asXML function returns string that cannot be casted as integer
Edit report at https://bugs.php.net/bug.php?id=61701&edit=1
ID: 61701
Updated by: aharvey@php.net
Reported by: petri dot peltoniemi at prosys dot fi
Summary: asXML function returns string that cannot be casted
as integer
-Status: Open
+Status: Not a bug
Type: Bug
Package: SimpleXML related
Operating System: Linux / ubuntu
PHP Version: 5.3.10
Block user comment: N
Private report: N
New Comment:
asXML() returns either a full XML document or an XML fragment for an element or
attribute. Either way, it's not going to look like a numeric string, since
there'll be an enclosing element or attribute definition, and therefore a typecast
to integer will fail.
Previous Comments:
------------------------------------------------------------------------
[2012-04-12 13:58:08] petri dot peltoniemi at prosys dot fi
Description:
------------
---
From manual page: http://www.php.net/simplexmlelement.asxml
---
I have array of arrays like
$result = array();
foreach ($wholesalers as $wholesaler) {
$result[] = array("Saldo" => $wholesaler->Available->asXML());
}
Available tag contains numeric value (integer) that should now be in string
This simplexml generated string cannot by typecasted to integer
Test script:
---------------
funtion SortSaldos($array1, $array2) {
$saldo1 = $array1['Saldo'];
$saldo2 = $array2['Saldo'];
echo "type: " . gettype($saldo1) . " $saldo1 as int " . intval($saldo1)
. " vs $saldo2 " . gettype($saldo2) . " as int " . intval($saldo2) .
"<br>";
echo "petri debug $saldo1 < $saldo2";
$saldo1 = intval($saldo1);
$saldo2 = intval($saldo2);
if ($saldo1 === $saldo2) {
echo " equal<br>";
return 0;
}
if ($saldo1 < $saldo2) {
echo " -1 <br>";
return -1;
} else {
echo " 1 <br>";
return 1;
}
}
uasort($result, "SortSaldos");
and with the function I see my debug information on screen:
type: string 1 as int 0 vs 2 string as int 0
petri debug 1 < 2 equal
type: string 9 as int 0 vs 1 string as int 0
petri debug 9 < 1 equal
type: string 1 as int 0 vs 4 string as int 0
petri debug 1 < 4 equal
so intval is always failing for strings generated by simplexml->asXML - function.
If I do my saldo with typecasting like this:
$result = array();
foreach ($wholesalers as $wholesaler) {
$result[] = array("Saldo" => (string) $wholesaler->Available);
}
everything works like expected
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=61701&edit=1
Thread (2 messages)