对xml结果的操作返回0

时间:2012-08-07 23:23:04

标签: php

我的主页遇到问题所以我做了一个测试页来找到问题所在。我正在通过XML和任何播放时间超过.1但不到1小时的游戏抓住时间,我试图乘以60。

它捕获了数字,但它为它们返回0。任何大于1的东西都不会经过if。

为什么不正确地进行操作?我怎样才能解决这个问题?     

$games = $gamespage->games;
$hoursplayed = 0;

    foreach($games->game as $game)
    {
    $hoursplayed = $game->hoursOnRecord;
    if ($hoursplayed < 1 && $hoursplayed >= .1) {
    $hoursplayed = $hoursplayed * 60; }
    echo $game->name, "<br />";
    echo $hoursplayed, "<br /><br />";
    } 

1 个答案:

答案 0 :(得分:1)

您可能需要首先cast这些值,否则这些是对象,您无法在PHP中对象进行倍增:

$hoursplayed = 0;

foreach ($gamespage->games->game as $game)
{
    $hoursplayed = (float) $game->hoursOnRecord;
    if ($hoursplayed < 1 && $hoursplayed >= .1) 
    {
         $hoursplayed = $hoursplayed * 60; 
    }
    echo $game->name, "<br />", $hoursplayed, "<br /><br />";
}