Why does PHP return zero when subtracting elements from an object?

0

I noticed a PHP behavior I did not know, it is the following:

For example, I'm getting an XML and using the simplexml_load_string() function. See below (ignore the lack of checks, just an example):

$simplexml_load_string = simplexml_load_string($my_xml_url);

if(isset($simplexml_load_string->item))
{
    $myarray = array();
    foreach($simplexml_load_string->item as $key => $value){
        $myarray['myelement'] = $value->myelement;  // 0 or 0.0
        $myarray['myelement2'] = $value->myelement2; // 0.000091
    }
}

So far I have an array $myarray with the key element myelement and the key element myelement2 with the values that can see commented in the code above, which are respectively "$ value-> myelement == 0 or 0.0 "and" $ value- > myelement2 == 0.000091 ". Now I'll try to make an account with the elements of $myarray below.

$result = $myarray['myelement2'] - $myarray['myelement'];
// $result recebe 0
So I did some testing to understand what was happening, first checked the accuracy in php.ini, it was OK, then I gave it a echo 0.000091 - 0; or echo 0.000091 - 0.0; and got 0.000091 as expected. So I checked the $myarray and realized that the array elements were not common values, but "SimplexXMLElement Object ([0] = > 0.000091"), which makes all sense because it assigns an element of the object returned by the xml , but I still did not understand what happened to always receive 0 (and I still do not know). But I decided to do the test below:

$simplexml_load_string = simplexml_load_string($my_xml_url);

if(isset($simplexml_load_string->item))
{
    $myarray = array();
    foreach($simplexml_load_string->item as $key => $value){
        $myarray['myelement'] = (float)$value->myelement;  // 0 or 0.0
        $myarray['myelement2'] = (float)$value->myelement2; // 0.000091
    }
}
$result = $myarray['myelement2'] - $myarray['myelement'];
// $result recebe 0.000091

And my problem has been solved, but the doubt continues, because PHP returns me 0 when it is a "SimplexXMLElement Object ([0] = > 0.0)" and returns me the expected value when it is a "float"?

    
asked by anonymous 22.02.2018 / 19:34

0 answers