Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP calculation

Tags:

html

php

math

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;
?>
like image 965
usertest Avatar asked Feb 28 '23 00:02

usertest


1 Answers

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.

like image 126
Sampson Avatar answered Mar 08 '23 05:03

Sampson