我有以下if语句,这些语句收集了在显示之前选定的所有日期。

$daysUsed = "";

if($this->dayweeksunsession==1){
    $daysUsed .= "Su ";
}

if($this->dayweekmonsession==1){
    $daysUsed .=  "M ";
}

if($this->dayweektuessession==1){
    $daysUsed .=  "T ";
}

if($this->dayweekwedsession==1){
    $daysUsed .=  "W ";
}

if($this->dayweekthurssession==1){
    $daysUsed .=  "Th ";
}

if($this->dayweekfrisession==1){
    $daysUsed .=  "F ";
}

if($this->dayweeksatsession==1){
    $daysUsed .=  "Sa ";
}

if($daysUsed !=="") {
    echo "</span><br/>Days of the Week: <span class='BoxReviewOutput'>";
    echo $daysUsed;
}

我的问题是,我怎样才能做到这一点,以便除最后一天外,每周的每一天都显示逗号。
例如:选择星期日和星期二。所以会显示“Su,T”
提前谢谢!

最佳答案

在ifs中添加一个逗号:

$daysUsed = "Whatever, ";

然后在输出最后一个字符串之前:
$daysUsed = substr($daysUsed, 0, -2);
编辑:-1必须是-2,以说明两天之间的间隔。

关于php - 除最后一个日期外,其他日期以逗号显示,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/10111579/

10-10 03:34