如何使while循环的最后一次交互在PHP中表现不同?

如何使while循环的最后一次交互在PHP中表现不同?,php,while-loop,Php,While Loop,下面的代码循环通过$count变量,只要小于14,代码就会增加。奇数用文字表示,偶数用数字表示,用逗号分隔 我希望循环中的最后一个数字(14)以句点(.)而不是逗号(,)结尾,我该怎么做 代码: <?php $count = 0; while ($count<=14){ if ($count&1) { $numword = new NumberFormatter("en", NumberFormatter::

下面的代码循环通过
$count
变量,只要小于14,代码就会增加。奇数用文字表示,偶数用数字表示,用逗号分隔

我希望循环中的最后一个数字(14)以句点(.)而不是逗号(,)结尾,我该怎么做

代码:

<?php
       $count = 0;
       while ($count<=14){
         if ($count&1) {
           $numword = new NumberFormatter("en", NumberFormatter::SPELLOUT);
           echo $numword->format($count).", ";
       } elseif ($count%2==0) {
            echo "{$count}, ";
        } else {
          echo "{$count}, ";
        }
        $count++;
       }
       ?>

你有几个选择。首先,创建一个输出字符串并附加到它,而不是在循环中进行
echo
ing。然后是尾随逗号和空格,并附加一个

$count = 0;
$output = '';
while ($count<=14){
    if ($count&1) {
        $numword = new NumberFormatter("en", NumberFormatter::SPELLOUT);
        $output .= $numword->format($count).", ";
    } else {
        $output .= "{$count}, ";
    }
    $count++;
}
echo trim($output, ', ') . ".";
第三个选项(对代码的最小更改)是根据
$count
的值输出分隔符:

$count = 0;
while ($count <= 14){
    if ($count&1) {
        $numword = new NumberFormatter("en", NumberFormatter::SPELLOUT);
        echo $numword->format($count);
    } else {
        echo $count;
    }
    echo ($count < 14) ? ', ' : '.';
    $count++;
}

您有几个选择。首先,创建一个输出字符串并附加到它,而不是在循环中进行
echo
ing。然后是尾随逗号和空格,并附加一个

$count = 0;
$output = '';
while ($count<=14){
    if ($count&1) {
        $numword = new NumberFormatter("en", NumberFormatter::SPELLOUT);
        $output .= $numword->format($count).", ";
    } else {
        $output .= "{$count}, ";
    }
    $count++;
}
echo trim($output, ', ') . ".";
第三个选项(对代码的最小更改)是根据
$count
的值输出分隔符:

$count = 0;
while ($count <= 14){
    if ($count&1) {
        $numword = new NumberFormatter("en", NumberFormatter::SPELLOUT);
        echo $numword->format($count);
    } else {
        echo $count;
    }
    echo ($count < 14) ? ', ' : '.';
    $count++;
}
我的解决方案是

<?php
$count = 0;
$string = '';
while ($count <= 14) {
    if ($count & 1) {
        $numword = new NumberFormatter("en", NumberFormatter::SPELLOUT);
        $string .= $numword->format($count) . ", ";
    } elseif ($count % 2 == 0) {
        $string .= "{$count}, ";
    } else {
        $string .= "{$count}, ";
    }
    $count++;
}
echo substr($string, 0, -2).'.';
我的解决方案是

<?php
$count = 0;
$string = '';
while ($count <= 14) {
    if ($count & 1) {
        $numword = new NumberFormatter("en", NumberFormatter::SPELLOUT);
        $string .= $numword->format($count) . ", ";
    } elseif ($count % 2 == 0) {
        $string .= "{$count}, ";
    } else {
        $string .= "{$count}, ";
    }
    $count++;
}
echo substr($string, 0, -2).'.';

@Ladi添加了第三个解决方案,它对原始代码的更改更少。注意:我简化了代码,删除了每种情况下不必要的
elseif
。@Nick。非常感谢你!最后,我删除了测试偶数的elseif条件,并将其替换为($count==14){echo{$count}.}@Ladi添加了第三个解决方案,该解决方案对原始代码的更改更少。注意:我简化了代码,删除了每种情况下不必要的
elseif
。@Nick。非常感谢你!最后,我删除了测试偶数的elseif条件,并将其替换为($count==14){echo{$count}