Write a php script to print letters from a to z

Last update on August 19 2022 21:50:29 [UTC/GMT +8 hours]

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

Write a PHP script to print letters from ‘a’ to ‘z’.

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.

How to print letter in PHP?

All alphabetic characters in an array can be achieved by using chr[], range[] with for and foreach loop in PHP. To display the array elements as output we can use echo, print_r[] and var_dump[] function.

How do I write a PHP script?

How to create a PHP script.
Line 1 – This tag tells the server that you are writing PHP code..
Line 2 – You can use the echo function to print out a string of text, this will be displayed back when the script is run..
Line 3 – This tag tells the server that you have stopped writing PHP code..

What is a PHP script?

PHP [Hypertext Preprocessor] is known as a general-purpose scripting language that can be utilized to create intuitive and dynamic websites. It was among the pioneer server-side language that can be integrated into HTML, making it easier to include functionality to web pages without requiring to call external data.

How do I start a PHP script?

Step 1: First of all, open the Apache Friends website and download XAMPP for Windows, and install it. Step 2: Start the XAMPP Program Control Panel. Click on the “Start” button next to the “Apache” to start your Apache Web Server. Also, start “MySQL” if your PHP programs depend on a MySQL database to run.

Chủ Đề