4

I'm trying to output a value from xml this is what I do -

<?php
    echo $responseTemp->Items->Item->CustomerReviews->AverageRating;
?>

This outputs 4.5, but when I change it to the code below it displays as 8. Why isn't it displaying as 9? Thanks.

<?php
echo $responseTemp->Items->Item->CustomerReviews->AverageRating*2;
?>
1
  • could you try echo (float)$responseTemp->Items->Item->CustomerReviews->AverageRating * 2 Commented Nov 25, 2009 at 17:58

2 Answers 2

6

Try casting the value to a numerical value first.

$num = (double) $responseTemp->Items->Item->CustomerReviews->AverageRating;

echo $num * 2;

See Type Juggling and String Conversion to Numbers on the PHP website for more information.

Sign up to request clarification or add additional context in comments.

1 Comment

Yeah, if you are expecting a double, enforce that it will be.
2

If you are looking for a decimal value without doing typecasting, you have to multiply by a number with a decimal. Otherwise it will return a regular integer like the number you gave it.

Try multiplying by 2.0

echo $responseTemp->Items->Item->CustomerReviews->AverageRating*2.0;

2 Comments

Enforcing the expectation on the known number seems less useful than making sure that the variable is what you expect it to be, e.g. echo (double) $responseTemp->Items->Item->CustomerReviews->AverageRating*2.0;
I know it's not proper, I was just offering another explanation as to why it wasn't working.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.