1

I have very little experience working with classes and object. I work in a loosely typed language, PHP.

I was working with a SimpleXML object and ran into a problem where I was trying to do math with an element of that object like $results->ProductDetail->{'Net'.$i};

If I echoed that value, I'd get 0.53 but when I tried to do math with it, it was converted to 0

Is there a reason that a loosely typed language would not recognize that as a float and handle it as such? Why would "echo" handle it as a string but the math fail to convert it?

Example:

$xml='<?xml version="1.0" encoding="UTF-8" ?>';
$xml.='<Test>
  <Item>
    <Price>0.53</Price>
  </Item>
</Test>';

$result=simplexml_load_string($xml);

var_dump($result->Item->Price);
echo '<br>';
echo $result->Item->Price;
echo '<br>';
echo 1+$result->Item->Price;
echo '<br>';
echo 1+(float)$result->Item->Price;

Output:

object(SimpleXMLElement)#4 (1) { [0]=> string(4) "0.53" } 
0.53
1
1.53

No object version:

$no='.53';
echo 1+$no;

Output:

1.53

================================

Side note: PHP strpos() does not properly convert an integer needle $i into a string.

$x=101; $i=1; if(strpos($x,"$i")===FALSE){echo $i." missing";}

and

$x=101; $i=1; if(strpos($x,$i)===FALSE){echo $i." missing";}

give different results. I don't want to become a php hater, but I'm beginning to lose that loving feeling.

7
  • This is not a request for a solution. I'd have asked that on SO, but I found if I cast it to string or float either one, it works. This is a request to more fully understand the WHY.
    – TecBrat
    Oct 29, 2013 at 16:54
  • 1
    The starting point for exploring mysteries like this in PHP is - in my experience - this article: me.veekun.com/blog/2012/04/09/php-a-fractal-of-bad-design
    – glenatron
    Oct 29, 2013 at 18:03
  • 1
    Could the element have leading space? A SSCCE that demonstrates this behavior (preferably without using objects etc.) would be helpful here.
    – amon
    Oct 29, 2013 at 20:13
  • @amon Why "preferably without using objects...?" The question is specific to how PHP handles the object. I did post some sample code.
    – TecBrat
    Oct 30, 2013 at 12:33
  • @glenatron, see my edit called "side note"
    – TecBrat
    Nov 5, 2013 at 20:41

1 Answer 1

2
echo $result->Item->Price;

That will output a string variable.

echo 1+$result->Item->Price;

That will convert the string "0.53" to integer. PHP does not round up. So it becomes 0 and 1+0=1.

echo 1+(float)$result->Item->Price;

That works because you're telling PHP to cast to a float. PHP will then auto convert the 1 from an int to float.

Alternatively, you can also do this.

echo 1.0+$result->Item->Price;

That will tell PHP to convert the string to a float as well.

This is all part of PHP's type juggling.

https://www.php.net/manual/en/language.types.type-juggling.php

Your issues are specific to string type conversion.

https://www.php.net/manual/en/language.types.string.php#language.types.string.conversion

One of the reasons PHP can do these things is because the + operator is always mathematical. Where in other languages it performs string concatenation. Since + always performs addition PHP attempts to convert string types to numeric types to honor the + operation.

UPDATE: Problems specific to SimpleXMLElement

There is a known bug in PHP when using the extension SimpleXML. PHP will cast strings that represent float values to int instead of float. The automatic type juggling is not working correctly.

Here is a bug report for the issue: https://bugs.php.net/bug.php?id=54973

For now I recommend casting to (float) whenever you use SimpleXML and expect a float value. That should not break anything when the fix is pushed out in the future.

8
  • +1, Good info, but I think there may be something even more subtle going on. When I tried his example assigning "0.53" to both a variable and to an attribute of a stdClass object, the addition("1 + $a", and "1 + $o->price") worked as expected (1.53). The probem only occurred with the xml object. Nov 5, 2013 at 22:26
  • 1
    echo 1.0+$result->Item->Price;' OUTPUT: 1. Considering that it might be specific to SimpleXML, I tried this: $stdobj= new stdClass; $stdobj->Price='.53'; echo 1 + $stdobj->Price;` and the output was 1.53 so it seems your are correct that it is specific to the SimpleXMLElement.
    – TecBrat
    Nov 6, 2013 at 13:44
  • 1
    I don't know anything about PHP extensions written in C but this looks like it should be supporting type juggling. I'm thinking we've found a bug.
    – Reactgular
    Nov 6, 2013 at 14:03
  • 1
    Mathew, please incorporate the relevant parts of your last three comments into your answer and I'll accept it. Thank you very much.
    – TecBrat
    Nov 6, 2013 at 14:16
  • 1
    bugs.php.net/bug.php?id=54973 "2013-07-22 The referenced pull request (#213) has two tests to verify the fix for this bug (when and if ready). Please don't forget to use them when commiting the fix."
    – TecBrat
    Nov 6, 2013 at 14:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.