w3resource

PHP String Exercises: Print letters from 'a' to 'z'


26. Print Letters from 'a' to 'z'

Write a PHP script to print letters from 'a' to 'z'.

Visual Presentation:

PHP String Exercises: Print letters from 'a' to 'z'

Sample Solution:

PHP Code:

<?php
for ($x = ord('a'); $x <= ord('z'); $x++)
// Loop through ASCII values of lowercase letters from 'a' to 'z'.

 echo chr($x);
// Convert ASCII value to corresponding character and echo.

 echo "\n";
// Output newline character.
?>

Sample Output:

abcdefghijklmnopqrstuvwxyz

Explanation:

The above PHP code snippet generates and echoes all lowercase letters from 'a' to 'z' using ASCII values:

  • The "for" loop iterates through ASCII values of lowercase letters from 'a' to 'z'.
  • ord('a') returns the ASCII value of 'a', and ord('z') returns the ASCII value of 'z'.
  • Inside the loop, chr($x) converts each ASCII value back to its corresponding character and echoes it.
  • After the loop, echo "\n"; outputs a newline character to format the output.

So, the output of this code would be all lowercase letters from 'a' to 'z' on a single line.

Flowchart :

Flowchart: Print letters from 'a' to 'z'

For more Practice: Solve these Related Problems:

  • Write a PHP script to generate an array of letters from 'a' to 'z' using range() and then output the concatenated string.
  • Write a PHP function to print all lowercase alphabet letters by iterating with a for loop and using chr() with ASCII values.
  • Write a PHP program to create a string of the alphabet using a loop that increments the ASCII code from 97 to 122, then display it.
  • Write a PHP script to output the alphabet in reverse order by generating the range and then reversing the array before joining the letters.

Go to:


PREV : Remove Commas from Numeric String.
NEXT : PHP Math functions Exercises Home.

PHP Code Editor:



Have another way to solve this solution? Contribute your code (and comments) through Disqus.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.