Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP Increment Alphabetically Looping

Tags:

php

i have code like this.

$str = 'A';
for ($i = 1;; $i++) {
    $count = ++$str;
    echo 'James said ' . $str.'<br>';
    echo 'Lia said ' . $str.'<br>';
    echo 'Ben said ' . $str.'<br>';
    if ($str == 'D') {
        echo 'STOP';
        break;
    }
}
die();

And give me the result like this, which is not what i wanted.

James said B
Lia said B
Ben said B
James said C
Lia said C
Ben said C
James said D
Lia said D
Ben said D
STOP

Want i want is :

James said B
Lia said C
Ben said D
STOP

How can i have a result like that? Or maybe with some other method? Like using array?

Thank you so much for the help!

like image 362
Erick Nyoto Avatar asked Feb 16 '26 21:02

Erick Nyoto


1 Answers

You just need to increment it each time you use $str:

echo 'James said ' . ++$str . '<br>';
echo 'Lia said ' . ++$str . '<br>';
echo 'Ben said ' . ++$str . '<br>';
like image 177
scrowler Avatar answered Feb 19 '26 09:02

scrowler



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!