Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

The code below is in PHP and returns prices from a XML response file.

$price = $result->Items->Item->OfferSummary->LowestNewPrice->FormattedPrice; //lowest new price 
$listPrice = $result->Items->Item->Offers->Offer->OfferListing->Price->FormattedPrice; //list price

If I echo $price or $listPrice it works

I wish to get the difference between the two prices but I am getting NULL if I do

$savings = $listPrice - $price; or $savings = ($listPrice - $price);

Any assistance is welcome

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
208 views
Welcome To Ask or Share your Answers For Others

1 Answer

You're most likely trying to subtract a string from a string. You need to convert the values to a numeric type. If you're working with prices, you'll probably want floating point numbers:

$price = floatval($result->Items->Item->OfferSummary->LowestNewPrice->FormattedPrice); //lowest new price 
$listPrice = floatval($result->Items->Item->Offers->Offer->OfferListing->Price->FormattedPrice); //list price

$savings = $listPrice - $price;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...