php在循环中连接

时间:2012-09-03 05:08:06

标签: php loops concatenation

我的代码上有这些

$from_m = 1;
$to_m = 6;

while( $from_m <= $to_m )
{
    if($from_m == 1)
        $m = "January";

    if($from_m == 2)
        $m = "February";

    if($from_m == 3)
        $m = "March";


    $add = "OR month = '$m' ";

    $from_m++;
}
结果

$add = "OR month = 'March' ";

我想要的是......

 $add = "OR month = 'January' OR month = 'February' OR month = 'March' ";

我该怎么做?

任何帮助?

3 个答案:

答案 0 :(得分:4)

替换此

$add = "OR month = '$m' ";

$add .= "OR month = '$m' ";

答案 1 :(得分:0)

$from_m = 1;
$to_m = 6;

while( $from_m <= $to_m )
{
    if($from_m == 1)
        $m = "January";

    if($from_m == 2)
        $m = "February";

    if($from_m == 3)
        $m = "March";


    $add .= "OR month = '$m' ";

    $from_m++;
}

答案 2 :(得分:-1)

使用:

$add .= "OR month = '$m' ";

而不是

$add = "OR month = '$m' ";