I am trying auto-increment using PHP to generate English alphabets instead numbers. I know how to do auto increment for numbers:
for ($i = 0; $i <= 2; $i++) {
echo $i;
}
But I want a way I can generate ABC instead 123.
You can use chr function together with ASCII code to generate them
For UpperCase:
for ($i = 65; $i <= 90; $i++) {
echo chr($i) . PHP_EOL;
}
For LowerCase:
for ($i = 97; $i <= 122; $i++) {
echo chr($i) . PHP_EOL;
}
Here the complete list of ASCII codes: https://www.ascii-code.com/
Just get the ascii code for A and loop for 26
<?php
$a_is = ord('A');
for ( $ch=$a_is; $ch<$a_is+26; $ch++ ) {
echo chr($ch) . PHP_EOL ;
}
Or set a char count
<?php
$a_is = ord('A');
$stop = 5;
for ( $ch=$a_is; $ch<$a_is+$stop; $ch++ ) {
echo chr($ch) . PHP_EOL ;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With