PHP中的数字添加不正确


Numbers not adding correctly in PHP

我试图添加一个Twitter计数和一个Feedburner计数,但当我将两者与基本加法结合时,它失败了!如果我拿到valkues并单独打印,效果很好,只是不让我加上这两个。。。

$twitCnt = twitter_subscribers(TWITTER_USERNAME);
$feedCnt = feed_subscribers(FEEDBURNER_USERNAME);
$totalCnt = $twitCnt + $feedCnt;
echo $totalCnt;

假设$twitCnt=2000并且$feedCnt=1000

现在,当我尝试添加2而不是获得3000时,我将获得$feedCnt值+1=1001而不是3000

我目前很困惑,如果我打印$twitCnt$feedCnt,它们会显示正确的数量,但是当我在代码中添加2时,$twitCnt会显示为=1,而不是它的实际值。

有什么想法会导致这种情况吗?


运行var_dump($twitCnt, $feedCnt) 后更新

string(5) "3,000"
object(SimpleXMLElement)#238 (1) {
  [0]=>
  string(5) "1,000"
}

还有获取统计数据的函数。。。

function twitter_subscribers($username = 'yourname'){
    $count = get_transient('twitter_count');
    if ($count != false){
        return $count;
    }else{
        $count = 0;         
        $url = 'http://api.twitter.com/1/users/show.xml?screen_name='. $username;
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_URL, $url);
        $data = curl_exec($ch);
        curl_close($ch);
        $xml = new SimpleXMLElement($data);
        $count = $xml->followers_count;
        $count = (float) $count;
        $count = number_format($count);
        set_transient('twitter_count', $count, 21600); // 6 hour cache
        return $count;
    }
}
function feed_subscribers($username = 'yourname')
{
    $feed_url = 'http://feeds.feedburner.com/' . $username;
    $count = get_transient('rss_count');
    if ($count != false)
        return $count;
    $count = 0;
    $data = wp_remote_get('http://feedburner.google.com/api/awareness/1.0/GetFeedData?uri=' .
        $feed_url . '');
    if (is_wp_error($data)) {
        return 'error';
    } else {
        $body = wp_remote_retrieve_body($data);
        echo $body;
        $xml = new SimpleXMLElement($body);
        $status = $xml->attributes();
        if ($status == 'ok') {
            $count = $xml->feed->entry->attributes()->circulation;
        } else {
            $count = 300; // fallback number
        }
    }
    set_transient('rss_count', $count, 21600); // 6 hour cache//60*60*24
    return $count;
}

好吧,第一个$twitCnt是一个格式化的字符串(我不知道为什么要将值直接格式化到变量中,因为在显示它之前需要计算该值,所以只应该在显示它时格式化它)。并且CCD_ 12是类型为CCD_。

要解决此问题,请不要格式化$twitCnt,而是仅在向用户显示值(即回显值)时才格式化。并修复这行$count = $xml->feed->entry->attributes()->circulation;,使其返回一个数值而不是一个对象。

**更新**

我在这里找到了一小段代码,并将其作为将格式化的数字转换为实际数值的解决方案提供给您。

例如,它将把1,200.123转换成1200.123

function formattedStringToNumeric($str) {
  $a = array_pad(explode(".",str_replace(',','',$str)), 2, 0);            // Split the string, using the decimal point as separator
  $int = $a[0];                        // The section before the decimal point
  $dec = $a[1];                        // The section after the decimal point
  $lengthofnum=strlen($dec);            // Get the num of characters after the decimal point
  $divider="1";                        // This sets the divider at 1
  $i=0;
  while($i < ($lengthofnum)) {
    $divider.="0";                    // Adds a zero to the divider for every char after the decimal point
    $i++;
  }
  $divider=(int)$divider;                // Converts the divider (currently a string) to an integer
  return $int+($dec/$divider);        // compiles the total back as a numeric 
}

但是,当你可以简单地做的时候,为什么要变得复杂呢:

$value = (float) str_replace(',','',$string);

问题就在这里:

$count = $xml->feed->entry->attributes()->circulation;

SimpleXMLElement::attributes返回一个SimpleXMLElement对象,获取其中一个属性也是如此,但您可以像在twitter函数中那样转换为预期的数据类型:

$count = $xml->followers_count;
$count = (float) $count;

因此,强制转换为integer、float或甚至字符串:

$count = (int) $xml->feed->entry->attributes()->circulation;

确保变量是数字。

$totalCnt = (int)$twitCnt + (int)$feedCnt;