w3resource

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

PHP String: Exercise-26 with Solution

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'

PHP Code Editor:

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

Previous: Write a PHP script to remove comma(s) from the specified numeric string.
Next: PHP Math functions Exercises Home.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/php-exercises/php-string-exercise-26.php