我试图找到一种基于百分比显示“是”和“否”的简单方法。 这只是一个例子,但在vb.net上,我可以设置静态并做这样的事情(70%和30%)
Static iNo As Long, iYes As Long
If iNo > iYes * 0.42 Then
iYes = iYes + 1
Return False 'this is the 70%
Else
iNo = iNo + 1
Return True 'this is the 30%
End If
我究竟能用PHP完成这么简单的事情吗?
答案 0 :(得分:1)
$numVotes = 100;
$trueVotes = 70;
$falseVotes = 30;
function addVote($boolVote){
$numVotes++;
if($boolVote)
$trueVotes++:
return $trueVotes * $numVotes / 100; //percent of true votes
}
else{
$falseVotes++;
return $falseVotes * $numVotes / 100; //percent of false votes
}
$newVote = true;
$percent = addVote($newVote);
($newVote)$strVote = "true":$strVote = "false";
echo sprintf("The vote is %s and the percent is %01.2f", $strVote, $percent);