Looping though A-Z with Do while loop in PHP

print a to z in php using for loop
php while loop
how to print az in php
php while loop multiple conditions
php for loop
while loop array php
while inside while in php
php alphabet increment

I was trying to loop though a-z with a do while loop. I know that I also can do that with foreach and forloop.

$char = 'a';
do {
   echo $char;

} while ($char <= 'z');

Why is that giving the output:


instead of just:


From the documentation:

PHP follows Perl's convention when dealing with arithmetic operations on character variables and not C's. For example, in PHP and Perl $a = 'Z'; $a++; turns $a into 'AA', while in C a = 'Z'; a++; turns a into '[' (ASCII value of 'Z' is 90, ASCII value of '[' is 91). Note that character variables can be incremented but not decremented and even so only plain ASCII alphabets and digits (a-z, A-Z and 0-9) are supported.

Try something like this:

for($i = 0, $char = 'a'; $i < 26; $i++, $char++) {
   echo $char;

How to iterate alphabets a-z in php?, Simply use range function of PHP foreach(range('a','z') as $v){ echo "$v \n"; }. Looping though A-Z with Do while loop in PHP. Tag: php. I was trying to loop though a-z with a do while loop. I know that I also can do that with foreach and forloop.



$char = 'z';

var_dump(++$char); //string(2) "aa"

var_dump('aa' <= 'z'); //bool(true)
var_dump('za' <= 'z'); //bool(false)


Personally I'd just use a loop from 97 (ascii value for a) to 122 (ascii value for z):

for ($i = 97; $i <= 122; $i++) {
    echo chr($i);

The while loop in PHP, In order to execute the block of code at least once, even the condition is false, you may use the do..while loop. An example of using the while loop to display  Loops are used to execute the same block of code again and again, as long as a certain condition is true. In PHP, we have the following loop types: while - loops through a block of code as long as the specified condition is true. dowhile - loops through a block of code once, and then repeats the loop as long as the specified condition is true.

You cannot compare z with 26 or some kind of number. You need something to compare it with the number. The function ord() does it. So, you can do something like:

$char = 'a';
do {
   echo $char;

} while (ord($char) <= ord('z'));

PHP for loop: Explained with 6 examples, PHP supports different types of loops to iterate through a given block of code. These include for loop, while and do while and the foreach loop. The for loop is the  PHP - Do While Loop. A "do while" loop is a slightly modified version of the while loop. If you recal from one of the previous lessons on While Loops the conditional statement is checked comes back true then the code within the while loop is executed. If the conditional statement is false then the code within the loop is not executed.

An alternative to the above options is the code below.

    $i = '0';
    while($i < '26') {
    echo chr(97 + $i);

PHP and MySQL by Example, 2 As long as the end of file has not been reached, the while loop will continue to execute. 3 For each iteration of the loop, the fgets() function reads in a line of text. 2 Until the end of the file is reached, the while loop will continue looping, is a regular expression containing character classes using a range, [a-z] and [0-9]. Just a note about using the continue statement to forego the remainder of a loop - be SURE you're not issuing the continue statement from within a SWITCH case - doing so will not continue the while loop, but rather the switch statement itself.

do-while - Manual, do-while loops are very similar to while loops, except the truth expression is This means that the code inside of the loop will iterate once through before the  Causes an exit from the innermost WHILE loop. Any statements that appear after the END keyword, marking the end of the loop, are executed. CONTINUE Causes the WHILE loop to restart, ignoring any statements after the CONTINUE keyword. Remarks. If two or more WHILE loops are nested, the inner BREAK exits to the next outermost loop.

PHP While, Do-While, For and Foreach Loops, foreach — loops through a block of code for each element in an array. You will also learn how to loop through the values of array using foreach() loop at the end of  In PHP 5, when foreach first starts executing, the internal array pointer is automatically reset to the first element of the array. This means that you do not need to call reset() before a foreach loop. As foreach relies on the internal array pointer in PHP 5, changing it within the loop may lead to unexpected behavior.

PHP program print alphabets in PHP, In the below code first we are going to set the range between A to Z, then looping through each alphabet using foreach() loop. Such situations can be handled with the help of do-while loop. do statement evaluates the body of the loop first and at the end, the condition is checked using while statement. It means that the body of the loop will be executed at least once, even though the starting condition inside while is initialized to be false .