intval() in php returns empty string?

Can anybody explain to me why this (dubious, outdated and otherwise sinful) PHP code occasionally returns a string containing two asterisks characters one after the other? In other words, intval() occasionally returns an empty string.

What value avcost column could have had for that row (avcost is defined as double)?

$rows = mysql_query("SELECT avcost FROM singles" );
if (!$rows) return 5;
$buffer = "";

while ($row = mysql_fetch_row($rows))
{
    $avcost = intval($row[0]); // round it
    $buffer .= $avcost . "*";
}

echo $buffer; // Sometimes it echos something like: 5*0*3*0*34**4*5*

A full answer would include minimal corrections to the above code (without using something like “if ($avcost == “”)” 🙂 that guarantee no double asterisk in the output. Thank you.


Source: php

Leave a Reply