如何使用IF语句以1的增量自动增加数字


How do I auto increase a number in increments of one using an IF statement?

我正在尝试回显一个变量数,但我不确定最好的方法。我的问题是:

我有一系列'IF'条件,并相应地回显一个变量。

   if ($month == "8" || $month == "9" || $month == "10") {
    $comingseason   = 'winter';
    $elementsDue    = "Due October 31, " . $year . ", for our " . $comingseason . " issue No. 9";
}
else if ($month == "11" || $month == "12" || $month == "1") {
    $comingseason   = 'spring';
    $elementsDue    = "Due January 31, " . ++$year . ", for our " . $comingseason . " issue No. 9";
}
else if ($month == "2" || $month == "3" || $month == "4") {
    $comingseason   = 'summer';
    $elementsDue    = "Due April 30, " . $year . ", for our " . $comingseason . " issue No. 9";
}
else if ($month == "5" || $month == "6" || $month == "7") {
    $comingseason   = 'fall';
    $elementsDue    = "Due July 31, " . ++$year . ", for our " . $comingseason . " issue No. 9";
}

我想知道如何用下一个增量++1替换'issue No. 9'中的数字以输出'10'?我不想每次都回到代码中输入下一期的编号。我希望这个数字能自己增加,基本上是每个季度都增加,就像你看到的那样。

我正在考虑一个php会话,但据我所知,这将不是永久的。我需要这个数字即使在用户结束会话后也保持不变。

我怎样才能做到这一点?

在我看来,问题#是年和季度的函数:从某个基准年/季度开始,每个季度有一个问题。因此,像这样的东西应该自动计算它(您必须使用$base_year和$base_qtr来使其与您的问题编号匹配)。

function currentIssue() {
    $base_year = 2014;
    $base_qtr = 2;
    $now_year = intval(date('Y');
    $now_qtr = ceil(intval(date'm')/3);
    return ($now_year - $base_year)*4 + ($now_qtr - $base_qtr);
}

好吧,这似乎是一个非常简单的问题。我建议阅读一本好的入门级PHP编程书籍。话虽如此,您需要设置一个while循环和一些嵌套的if else条件。

$month = 0;
while ($month < 12) {
    $month++;
    If ($month > 7 && $month < 11)
    { 
        $comingsoon = "winter";
    }
    elseif 
    {
        some logic
    }
    elseif
    {
        some logic
    }
    else
    {
    }
} # end of while loop

我有意把剩下的代码留给你写。这将是一个很好的练习,让你学习一些编码:)祝你好运!

也许你正在寻找这个,

解释:添加一个整数变量,比如$issuNo,然后添加一个初始值。然后在if/else条件块后面加上增量变量,比如$ issuno++,然后你最好编写一个程序,可以在后面继续if/else语句。

$issuNo = 9;     
if ($month == "8" || $month == "9" || $month == "10") {
        $comingseason   = 'winter';
        $elementsDue    = "Due October 31, " . $year . ", for our " . $comingseason . " issue No. ".$issuNo;
    }
    else if ($month == "11" || $month == "12" || $month == "1") {
        $comingseason   = 'spring';
        $elementsDue    = "Due January 31, " . ++$year . ", for our " . $comingseason . " issue No. ".$issuNo;
    }
    else if ($month == "2" || $month == "3" || $month == "4") {
        $comingseason   = 'summer';
        $elementsDue    = "Due April 30, " . $year . ", for our " . $comingseason . " issue No. ".$issuNo;
    }
    else if ($month == "5" || $month == "6" || $month == "7") {
        $comingseason   = 'fall';
        $elementsDue    = "Due July 31, " . ++$year . ", for our " . $comingseason . " issue No. ".$issuNo;
    }
$issuNo++;

如果这是你正在寻找的:

<?php
$month = '3';
$year = '2015';
$issue = 9;
 if ($month == "8" || $month == "9" || $month == "10") {
    $comingseason   = 'winter';
 echo   $elementsDue    = "Due October 31, " . $year . ", for our " . $comingseason . " issue No. $issue";
}
else if ($month == "11" || $month == "12" || $month == "1") {
    $comingseason   = 'spring';
        $issue = $issue + 1;
echo    $elementsDue    = "Due January 31, " . ++$year . ", for our " . $comingseason . " issue No. $issue";
}
else if ($month == "2" || $month == "3" || $month == "4") {
    $comingseason   = 'summer';
        $issue = $issue + 2;
echo    $elementsDue    = "Due April 30, " . $year . ", for our " . $comingseason . " issue No. $issue";
}
else if ($month == "5" || $month == "6" || $month == "7") {
    $comingseason   = 'fall';
    $issue = $issue + 3;
echo    $elementsDue    = "Due July 31, " . ++$year . ", for our " . $comingseason . " issue No. $issue";
}
?>
PHP小提琴