PHP String: Exercise-26 with Solution
Write a PHP script to print letters from 'a' to 'z'.
Pictorial Presentation:
Sample Solution:
PHP Code:
Sample Output:
abcdefghijklmnopqrstuvwxyz
Flowchart :
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.
PHP: Tips of the Day
PHP - how to create a newline character?
Only double quoted strings interpret the escape sequences \r and \n as '0x0D' and '0x0A' respectively, so you want:
"\r\n"
Single quoted strings, on the other hand, only know the escape sequences \\ and \'.
So unless you concatenate the single quoted string with a line break generated elsewhere [e. g., using double quoted string "\r\n" or using chr function chr[0x0D].chr[0x0A]], the only other way to have a line break within a single quoted string is to literally type it with your editor:
$s = 'some text before the line break some text after';
Make sure to check your editor for its line break settings if you require some specific character sequence [\r\n for example].
Ref : //bit.ly/3hcyege
Introduction
PHP is a server scripting language, and It is a powerful tool for making interactive and dynamic Web-pages. I have used WampServer 2.2 for following example..
Result
Last updated Sep 10, 2018 0
In this tutorial, we’re going to write a PHP program to print alphabets from A to Z. It’s quite simple code using though PHP range[] Function.
PHP range[] Function
The range[] function creates an array containing a range of elements. This function returns an array of elements from low to high.
Syntax
range[low,high,step]
PHP program print alphabets from A to Z
In the below code first we are going to set the range between A to Z, then looping through each alphabet using foreach[] loop.
Output:
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
PHP program print alphabets from a to z
So we could write a similar program for lowercase characters in a similar way like in below example:
Output:
a b c d e f g h i j k l m n o p q r s t u v w x y z
PHP program print alphabets from Z to A in Reverse order
We could print alphabets from Z to A in Reverse order like in below example:
Output:
Z Y X W V U T S R Q P O N M L K J I H G F E D C B A
I made a constant time function as follows
This function gives the Alphabetic representation of a numeric index
public static $alpha = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'];
public static function getColName[$index]{
$index--;
$nAlphabets = 26;
$f = floor[$index/pow[$nAlphabets,0]] % $nAlphabets;
$s = [floor[$index/pow[$nAlphabets,1]] % $nAlphabets]-1;
$t = [floor[$index/pow[$nAlphabets,2]] % $nAlphabets]-1;
$f = $f < 0 ? '' : self::$alpha[$f];
$s = $s < 0 ? '' : self::$alpha[$s];
$t = $t < 0 ? '' : self::$alpha[$t];
return trim["{$t}{$s}{$f}"];
}
Now if you want to use it create a range. you can call this function in a loop pushing your values to an array.
As for most of the time, we need the representation rather than a range this function would work just fine.
HOW TO USE
Just enclose these static functions in a class and use it as
className::getColName[47];
Making a range in my case was a waste of memory.