Integer division in PHP generates zero - how to make it float?
In Russian card game I'm trying to save statistics on how often the player swears (says "bad words" - and we have a lot of them in Russian) in the following PostgreSQL table:
# select * from pref_chat order by swear desc;
id | swear | lines
-------------------------+-------+-------
OK194281930260 | 3 | 153
OK350321778615 | 2 | 127
DE12770 | 2 | 339
OK122898831181 | 2 | 63
OK349829847011 | 2 | 126
OK215240745969 | 1 | 66
OK459742722980 | 1 | 96
And I need to create an integer of this data - from 1 to 100 (overflow is ok) - so that I can create the following "swear'o'meter":
So, I'm trying (as of PHP 5.3 on CentOS 6.2):
$sth = $db->prepare('select swear, lines from pref_chat where id=?');
$sth->execute(array($id));
if ($row = $sth->fetch(PDO::FETCH_ASSOC)) {
$quotient = 100 * floor(20 * $row['swear'] / (1 + $row['lines']));
print('<p><img src="https://chart.googleapis.com/chart?chs=700x100&cht=gom&chco=00FF00,FFFF00,FF0000&chxt=x&chxl=0:|Swearometer&chd=t:' . $quotient . '" width=700 height=100 alt="Swearometer"></p>)';
}
Unfortunately I get zero because PHP is probably doing "integer division".
I tried adding floor () to $ row ['swear'] and $ row ['lines'] to "throw" them to float, but it didn't help.
UPDATE:
Sorry I had a typo in my original question ... The $ factor is indeed 0, I am printing it out. I've also tried the following, but it's still zero:
$quot = 100 * floor(20 * $row['swear'] / (.1 + $row['lines']));
+3
source to share
2 answers