html
iphone
c
xml
python
mysql
database
xcode
android
ruby-on-rails
objective-c
visual-studio
multithreading
silverlight
perl
facebook
mvc
php5
jsp
postgresql
Well, first $twitCnt is a formatted string (I don't know why you format the value directly into the variable, since you need to compute that value prior to display it, you should only format it when displaying it). And $feedCnt is an object of type SimpleXMLElement.
$twitCnt
$feedCnt
SimpleXMLElement
To fix this, do not format $twitCnt but only when you display the value (ie. echo it) to the user. And fix this line $count = $xml->feed->entry->attributes()->circulation; so it returns a numeric value instead of an object.
$count = $xml->feed->entry->attributes()->circulation;
** Update **
I took a small piece of code found here and giving it to you as a solution to convert your formatted numbers to actual numeric values.
For instance, it will convert 1,200.123 into 1200.123
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 }
But why go complicated when you can simply do :
$value = (float) str_replace(',','',$string);
The issue is here:
SimpleXMLElement::attributes returns a SimpleXMLElement object, and so does fetching one of the properties, but you can cast to the expected data type just like you did here in the twitter function:
SimpleXMLElement::attributes
$count = $xml->followers_count; $count = (float) $count;
So, cast to integer, float, or even string:
$count = (int) $xml->feed->entry->attributes()->circulation;
ensure, that the variables are numbers.
$totalCnt = (int)$twitCnt + (int)$feedCnt;