I have this function which takes the average user rating given to an item and converts it to a relevant class:
function get_user_rating_class($urate_ave){
if($urate_ave==='0.0'){
$user_rating_class='unrated_u';
}else{
$round_to = 0.5;
$rounded = round($urate_ave / $round_to) * $round_to;
$removedec = str_replace('.','',$rounded);
if(strlen($removedec)<2){$removedec.'0';}
$user_rating_class='rating_user_'+$removedec;
}
return $user_rating_class;
}
No when I put a value, say 3.0 into the function it should come out as rating_user_30 but instead it is coming out as rating_user_3, so there must be something wrong with this line:
if(strlen($removedec)<2){$removedec.'0';}
I thought it might be because It was treating the value $removedec as a number rather than a string so I tried adding this line just before it, but no change:
$removedec = strval($removedec);
Could someone tell me what I am doing wrong here.
You should use
.=instead of.to append a value to a string and set the variable to that value. The dot operator alone will append the value but not change the variable’s value.