我被困在一些PHP编码上,似乎最初很容易实现。这就是我想做的事情:
<?php
$amountOfDigits = 1;
$numbers = range(1,3);
shuffle($numbers);
for($i = 0;$i < $amountOfDigits;$i++)
$digits .= $numbers[$i];
while ( have_posts() ) : the_post();
static $count = 0;
if ($digits == '1') {
//Do this if statement
if ($count == "2") }
elseif ($digits == '2') {
//Do this if statement
if ($count == "2" || $count == "3") }
elseif ($digits == '3') {
//Do this if statement
if ($count == "2" || $count == "3" || $count == "4") }
{ //here the rest of the code
&GT;
因此,根据$ digits变量,if语句的格式设置为在//the rest of the code
上面的行上使用
如何正确地将它放入PHP中?
感谢。 Robbert
答案 0 :(得分:3)
如果我理解你的问题,你需要这样的东西:
if ($digits == '1')
$mycond = ($count == "2");
elseif ($digits == '2')
$mycond = ($count == "2" || $count == "3")
elseif ($digits == '3')
$mycond = ($count == "2" || $count == "3" || $count == "4")
然后你可以进一步使用
if($mycond){
// blahblahblah
}
答案 1 :(得分:3)
如果你想在每种情况下使用相同的执行块,那么有一个非常简单的解决方案。
您应该只使用一个根据“数字”检查“计数”状态的函数。
<?php
function checkCountAgainstDigits($count, $digits){
switch($digits){
case 1:
return $count === 1;
case 2:
return $count === 2 || $count === 3;
case 3:
return $count === 2 || $count === 3 || $count === 4;
default:
// The default case "ELSE"
return FALSE;
}
}
if(checkCountAgainstDigits($count, $digits)){
// do
}
?>
如果您想要另一个,那么您的解决方案是正确的。