我有一个类似于whois数据库的脚本。此函数返回站点视图,我想在值之间进行回显。
如何回显并返回一个结果?如果数字为4000,则应仅返回 1k-10k
行就像
1550330
百万
代码:
$siteTotalViews=1000000;
if($siteTotalViews <= 100){
echo '0-100';
}
if($siteTotalViews <= 1000){
echo '100-1k';
}
if($siteTotalViews <= 10000){
echo '1k-10k';
}
if($siteTotalViews <= 100000){
echo '10k-100k';
}
if($siteTotalViews <= 1000000){
echo '100k-1 mil';
}
if($siteTotalViews <= 2000000){
echo '1 mil-2 mil';
}
if($siteTotalViews <= 5000000){
echo '2 mil-5 mil';
}
if($siteTotalViews <= 10000000){
echo '5 mil-10 mil';
}
if($siteTotalViews >= 10000000){
echo '10 mil +';
}
答案 0 :(得分:2)
快速修复:
$siteTotalViews=1000000;
if($siteTotalViews <= 100){
echo '0-100';
}
//next else is new
else if($siteTotalViews <= 1000){
echo '100-1k';
}
//next else is new
else if($siteTotalViews <= 10000){
echo '1k-10k';
}
//next else is new
else if($siteTotalViews <= 100000){
echo '10k-100k';
}
更好的解决方法:
$names=array(
100 => '0-100',
1000 => '100-1k',
10000 => '1k-10k',
...
}
foreach ($names as $count=>$name)
if ($siteTotalViews<$count) break;
echo $name;
答案 1 :(得分:2)
您可以创建一个返回间隔的函数。当函数点击return语句时它会停止执行,因此你只能获得一个值。然后你可以调用函数并回显结果:
function getInterval($siteTotalViews) {
if($siteTotalViews <= 100){
return '0-100';
}
if($siteTotalViews <= 1000){
return '100-1k';
}
...
}
echo getInterval(1000);
答案 2 :(得分:2)
您可以将所有限制及其相应的文本放入数组中,然后遍历反向数组以查找适当的输出。 (当达到限制时break
循环
$siteTotalViews=1000000;
$outputs = array(
0 => '0-100',
100 => '100-1k',
1000 => '1k-10k',
10000 => '10k-100k',
100000 => '100k-1 mil',
1000000 => '1 mil-2 mil',
2000000 => '2 mil-5 mil',
5000000 => '5 mil-10 mil',
10000000 => '10 mil +' );
$outputs = array_reverse($outputs);
foreach ($outputs as $limit => $text) {
if ($siteTotalViews >= $limit) {
echo $text;
break;
}
}
答案 3 :(得分:0)
$siteTotalViews=1000000;
if($siteTotalViews >= 0 && $siteTotalViews <=100 ){
echo '0-100'; }
if($siteTotalViews >=101 && $siteTotalViews <= 1000){
echo '100-1k'; }
.....
if($siteTotalViews >= 10000000){
echo '10 mil +'; }