php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #61701 asXML function returns string that cannot be casted as integer
Submitted: 2012-04-12 13:58 UTC Modified: 2012-04-13 01:27 UTC
From: petri dot peltoniemi at prosys dot fi Assigned:
Status: Not a bug Package: SimpleXML related
PHP Version: 5.3.10 OS: Linux / ubuntu
Private report: No CVE-ID: None
 [2012-04-12 13:58 UTC] 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


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2012-04-13 01:27 UTC] aharvey@php.net
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.
 [2012-04-13 01:27 UTC] aharvey@php.net
-Status: Open +Status: Not a bug
 
PHP Copyright © 2001-2022 The PHP Group
All rights reserved.
Last updated: Tue Nov 29 13:05:54 2022 UTC