0

The code below loops through the $count variable and increases as long as it's less than 14. Odd numbers are written in words while even numbers are written in numbers, separated by commas.

I want the last number in the loop(14) to end with a period(.) instead of a comma(,), how do I do that?

Code:

<?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++;
       }
       ?>

Output:

0, one, 2, three, 4, five, 6, seven, 8, nine, 10, eleven, 12, thirteen, 14,
0

2 Answers 2

2

You have a few options. Firstly, create an output string and append to it rather than echoing in the loop. Then trim the trailing comma and space and append a .:

$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, ', ') . ".";

The other option is to write each element to an array and then implode it, adding a period on the end:

$count = 0;
$output = array();
while ($count<=14){
    if ($count&1) {
        $numword = new NumberFormatter("en", NumberFormatter::SPELLOUT);
        $output[] = $numword->format($count);
    } else {
        $output[] = $count;
    }
    $count++;
}
echo implode(', ', $output) . ".\n";

Your third option (minimal changes to your code) is to output a separator dependent on the value of $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++;
}

In all cases the output is:

0, one, 2, three, 4, five, 6, seven, 8, nine, 10, eleven, 12, thirteen, 14.

Demo on 3v4l.org

Sign up to request clarification or add additional context in comments.

2 Comments

@Ladi I added a third solution which requires less changes to your original code. Note I have simplified the code to remove the unnecessary elseif in each case.
@Nick. Thank you so much! I ended up just removing the elseif condition that tests for even numbers and replacing it with ($count==14){ echo "{$count}. " }
0

My solution is

<?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).'.';

hope this help :)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.