对于这种情况,数组会比开关盒更有效吗?
Would an array be more effective than a switch case for this scenario?
我根据 API 的输入显示星级(注意:仅显示,根本不接收评级)。下面的代码完全按照我的需要工作,但它是一个非常大的逻辑片段,感觉它可以被大量简化。
我在这里阅读了其他几张票,这些票表明使用数组可能比 switch case 更有效。但是因为逻辑取决于数字范围内 true 之间的每种情况(比如大于 2.5 但小于 3),我不确定在这种情况下数组是否有效。
底线是:能否以某种方式大量简化此代码?
$stars = 3.5;
switch ($stars) {
case ($stars > 1 && $stars <= 1.5):
$star2 = 'dashicons-star-half';
$star3 = 'dashicons-star-empty';
$star4 = 'dashicons-star-empty';
$star5 = 'dashicons-star-empty';
break;
case($stars > 1.5 && $stars <= 2):
$star2 = 'dashicons-star-filled';
$star3 = 'dashicons-star-empty';
$star4 = 'dashicons-star-empty';
$star5 = 'dashicons-star-empty';
break;
case($stars > 2 && $stars <= 2.5):
$star2 = 'dashicons-star-filled';
$star3 = 'dashicons-star-half';
$star4 = 'dashicons-star-empty';
$star5 = 'dashicons-star-empty';
break;
case($stars > 2.5 && $stars <= 3):
$star2 = 'dashicons-star-filled';
$star3 = 'dashicons-star-filled';
$star4 = 'dashicons-star-empty';
$star5 = 'dashicons-star-empty';
break;
case($stars > 3 && $stars <= 3.5):
$star2 = 'dashicons-star-filled';
$star3 = 'dashicons-star-filled';
$star4 = 'dashicons-star-half';
$star5 = 'dashicons-star-empty';
break;
case($stars > 3.5 && $stars <= 4):
$star2 = 'dashicons-star-filled';
$star3 = 'dashicons-star-filled';
$star4 = 'dashicons-star-filled';
$star5 = 'dashicons-star-empty';
break;
case($stars > 4 && $stars <= 4.5):
$star2 = 'dashicons-star-filled';
$star3 = 'dashicons-star-filled';
$star4 = 'dashicons-star-filled';
$star5 = 'dashicons-star-half';
break;
case($stars > 4.5):
$star2 = 'dashicons-star-filled';
$star3 = 'dashicons-star-filled';
$star4 = 'dashicons-star-filled';
$star5 = 'dashicons-star-filled';
break;
default:
$star2 = 'dashicons-star-empty';
$star3 = 'dashicons-star-empty';
$star4 = 'dashicons-star-empty';
$star5 = 'dashicons-star-empty';
}
?>
<div class="wporg-ratings" title="<?php echo $stars; ?> out of 5 stars" style="color:#e6b800;">
<span class="dashicons dashicons-star-filled"></span>
<span class="dashicons <?php echo $star2; ?>"></span>
<span class="dashicons <?php echo $star3; ?>"></span>
<span class="dashicons <?php echo $star4; ?>"></span>
<span class="dashicons <?php echo $star5; ?>"></span>
</div>
这是快速版本:
$stars = 3.5;
$d = array(
($stars >= 1.0) ? 'dashicons-star-filled' : (($stars >= 0.5) ? 'dashicons-star-half' : 'dashicons-star-empty'),
($stars >= 2.0) ? 'dashicons-star-filled' : (($stars >= 1.5) ? 'dashicons-star-half' : 'dashicons-star-empty'),
($stars >= 3.0) ? 'dashicons-star-filled' : (($stars >= 2.5) ? 'dashicons-star-half' : 'dashicons-star-empty'),
($stars >= 4.0) ? 'dashicons-star-filled' : (($stars >= 3.5) ? 'dashicons-star-half' : 'dashicons-star-empty'),
($stars >= 5.0) ? 'dashicons-star-filled' : (($stars >= 4.5) ? 'dashicons-star-half' : 'dashicons-star-empty')
);
echo '<div class="wporg-ratings" title="' . $stars . ' out of 5 stars" style="color:#e6b800;">';
foreach ($d as $value) echo '<span class="dashicons ' . $value . '"></span>';
echo '</div>';
和你原来的逻辑不一样(第一个星总是满的),但是你可以用它作为方向
下一步可能是将重复的代码移动到函数中
我认为不需要数组或开关。您可以使用 for 循环,并根据循环变量检查 $stars
值以查看应使用哪个图标。
<div class="wporg-ratings" title="<?php echo $stars; ?> out of 5 stars" style="color:#e6b800;">
<?php
// for loop: one iteration for each of five possible stars
// (for loops are generally best for repeating code a specific number of times)
for ($i=0; $i < 5; $i++) {
if ($stars <= $i ) {
// empty stars are displayed when the iterator (i) is >= the star value
echo '<span class="dashicons dashicons-star-empty"></span>';
} elseif ($stars <= $i + 0.5) {
// half stars are displayed for star values between i and i+0.5
echo '<span class="dashicons dashicons-star-half"></span>';
} else {
// whole stars are displayed when the star value is > i+0.5
echo '<span class="dashicons dashicons-star-filled"></span>';
}
}
?>
</div>
为了帮助理解为什么会这样,请通过循环获取理论值。我们可以使用您问题中的那个 3.5
.
- 第一次迭代:3.5 > 0、3.5 > 0.5,所以你得到 else 值(实心星)
- 第二次迭代:3.5 > 1、3.5 > 1.5,所以你得到 else 值(实心星)
- 第三次迭代:3.5 > 2、3.5 > 2.5,所以你得到了 else 值(实心星)
- 第四次迭代:3.5 > 3,3.5 = 3.5,得到elseif值(半星)
- 第五次迭代:3.5 < 4,所以你得到 if 值(空星)
另一种可能性是定义一个散列数组。假设数组中的每个元素都包含 "minimum value, and three strings," 并且数组按升序排序。您的 if/then 逻辑可以尽快替换为遍历该数组的循环,breaking-out。
但是: "Clarity trumps all other concerns." 如果代码 有效 并且可以合理维护,谁在乎它臭吗?
可以简化现有代码以消除任何值 "falling through the cracks" 的可能性,方法是注意 if-condition 的前半部分可以省略:如果达到第二种情况,则必须是true 值大于 1.5,依此类推。
我根据 API 的输入显示星级(注意:仅显示,根本不接收评级)。下面的代码完全按照我的需要工作,但它是一个非常大的逻辑片段,感觉它可以被大量简化。
我在这里阅读了其他几张票,这些票表明使用数组可能比 switch case 更有效。但是因为逻辑取决于数字范围内 true 之间的每种情况(比如大于 2.5 但小于 3),我不确定在这种情况下数组是否有效。
底线是:能否以某种方式大量简化此代码?
$stars = 3.5;
switch ($stars) {
case ($stars > 1 && $stars <= 1.5):
$star2 = 'dashicons-star-half';
$star3 = 'dashicons-star-empty';
$star4 = 'dashicons-star-empty';
$star5 = 'dashicons-star-empty';
break;
case($stars > 1.5 && $stars <= 2):
$star2 = 'dashicons-star-filled';
$star3 = 'dashicons-star-empty';
$star4 = 'dashicons-star-empty';
$star5 = 'dashicons-star-empty';
break;
case($stars > 2 && $stars <= 2.5):
$star2 = 'dashicons-star-filled';
$star3 = 'dashicons-star-half';
$star4 = 'dashicons-star-empty';
$star5 = 'dashicons-star-empty';
break;
case($stars > 2.5 && $stars <= 3):
$star2 = 'dashicons-star-filled';
$star3 = 'dashicons-star-filled';
$star4 = 'dashicons-star-empty';
$star5 = 'dashicons-star-empty';
break;
case($stars > 3 && $stars <= 3.5):
$star2 = 'dashicons-star-filled';
$star3 = 'dashicons-star-filled';
$star4 = 'dashicons-star-half';
$star5 = 'dashicons-star-empty';
break;
case($stars > 3.5 && $stars <= 4):
$star2 = 'dashicons-star-filled';
$star3 = 'dashicons-star-filled';
$star4 = 'dashicons-star-filled';
$star5 = 'dashicons-star-empty';
break;
case($stars > 4 && $stars <= 4.5):
$star2 = 'dashicons-star-filled';
$star3 = 'dashicons-star-filled';
$star4 = 'dashicons-star-filled';
$star5 = 'dashicons-star-half';
break;
case($stars > 4.5):
$star2 = 'dashicons-star-filled';
$star3 = 'dashicons-star-filled';
$star4 = 'dashicons-star-filled';
$star5 = 'dashicons-star-filled';
break;
default:
$star2 = 'dashicons-star-empty';
$star3 = 'dashicons-star-empty';
$star4 = 'dashicons-star-empty';
$star5 = 'dashicons-star-empty';
}
?>
<div class="wporg-ratings" title="<?php echo $stars; ?> out of 5 stars" style="color:#e6b800;">
<span class="dashicons dashicons-star-filled"></span>
<span class="dashicons <?php echo $star2; ?>"></span>
<span class="dashicons <?php echo $star3; ?>"></span>
<span class="dashicons <?php echo $star4; ?>"></span>
<span class="dashicons <?php echo $star5; ?>"></span>
</div>
这是快速版本:
$stars = 3.5;
$d = array(
($stars >= 1.0) ? 'dashicons-star-filled' : (($stars >= 0.5) ? 'dashicons-star-half' : 'dashicons-star-empty'),
($stars >= 2.0) ? 'dashicons-star-filled' : (($stars >= 1.5) ? 'dashicons-star-half' : 'dashicons-star-empty'),
($stars >= 3.0) ? 'dashicons-star-filled' : (($stars >= 2.5) ? 'dashicons-star-half' : 'dashicons-star-empty'),
($stars >= 4.0) ? 'dashicons-star-filled' : (($stars >= 3.5) ? 'dashicons-star-half' : 'dashicons-star-empty'),
($stars >= 5.0) ? 'dashicons-star-filled' : (($stars >= 4.5) ? 'dashicons-star-half' : 'dashicons-star-empty')
);
echo '<div class="wporg-ratings" title="' . $stars . ' out of 5 stars" style="color:#e6b800;">';
foreach ($d as $value) echo '<span class="dashicons ' . $value . '"></span>';
echo '</div>';
和你原来的逻辑不一样(第一个星总是满的),但是你可以用它作为方向
下一步可能是将重复的代码移动到函数中
我认为不需要数组或开关。您可以使用 for 循环,并根据循环变量检查 $stars
值以查看应使用哪个图标。
<div class="wporg-ratings" title="<?php echo $stars; ?> out of 5 stars" style="color:#e6b800;">
<?php
// for loop: one iteration for each of five possible stars
// (for loops are generally best for repeating code a specific number of times)
for ($i=0; $i < 5; $i++) {
if ($stars <= $i ) {
// empty stars are displayed when the iterator (i) is >= the star value
echo '<span class="dashicons dashicons-star-empty"></span>';
} elseif ($stars <= $i + 0.5) {
// half stars are displayed for star values between i and i+0.5
echo '<span class="dashicons dashicons-star-half"></span>';
} else {
// whole stars are displayed when the star value is > i+0.5
echo '<span class="dashicons dashicons-star-filled"></span>';
}
}
?>
</div>
为了帮助理解为什么会这样,请通过循环获取理论值。我们可以使用您问题中的那个 3.5
.
- 第一次迭代:3.5 > 0、3.5 > 0.5,所以你得到 else 值(实心星)
- 第二次迭代:3.5 > 1、3.5 > 1.5,所以你得到 else 值(实心星)
- 第三次迭代:3.5 > 2、3.5 > 2.5,所以你得到了 else 值(实心星)
- 第四次迭代:3.5 > 3,3.5 = 3.5,得到elseif值(半星)
- 第五次迭代:3.5 < 4,所以你得到 if 值(空星)
另一种可能性是定义一个散列数组。假设数组中的每个元素都包含 "minimum value, and three strings," 并且数组按升序排序。您的 if/then 逻辑可以尽快替换为遍历该数组的循环,breaking-out。
但是: "Clarity trumps all other concerns." 如果代码 有效 并且可以合理维护,谁在乎它臭吗?
可以简化现有代码以消除任何值 "falling through the cracks" 的可能性,方法是注意 if-condition 的前半部分可以省略:如果达到第二种情况,则必须是true 值大于 1.5,依此类推。