PHP Program to Check if a Given String is Pangram or Not
Last Updated :
23 Jul, 2025
A pangram is a sentence that contains every letter of the alphabet at least once. Checking whether a given string is a pangram is a common programming task. In this article, we will explore different approaches to creating a PHP program that checks if a given string is a pangram or not.
Approach 1: Using an Array
The first approach involves using an array to keep track of the presence of each letter in the alphabet.
PHP
<?php
function isPangram($str) {
// Convert the string to lowercase
// for case-insensitive comparison
$str = strtolower($str);
// Initialize an array to track the
// presence of each letter
$letters = array_fill(0, 26, false);
// Iterate through each character in
// the string
for ($i = 0; $i < strlen($str); $i++) {
$char = $str[$i];
// Check if the character is an
// alphabet letter
if (ctype_alpha($char)) {
// Mark the corresponding letter
// as present
$letters[ord($char) - ord('a')] = true;
}
}
// Check if all letters are present
// in the array
return !in_array(false, $letters);
}
// Driver code
$str = "The quick brown fox jumps over the lazy dog";
if (isPangram($str)) {
echo "String is a pangram.";
} else {
echo "String is not a pangram.";
}
?>
OutputString is a pangram.
Approach 2: Using Set
An alternative approach involves using a set to keep track of the unique letters present in the string.
PHP
<?php
function isPangram($str) {
// Convert the string to lowercase for
// case-insensitive comparison
$str = strtolower($str);
// Initialize an empty set to track
// unique letters
$lettersSet = [];
// Iterate through each character in
// the string
for ($i = 0; $i < strlen($str); $i++) {
$char = $str[$i];
// Check if the character is an
// alphabet letter
if (ctype_alpha($char)) {
// Add the letter to the set
$lettersSet[$char] = true;
}
}
// Check if the set contains all 26 letters
return count($lettersSet) == 26;
}
// Driver code
$str = "The quick brown fox jumps over the lazy dog";
if (isPangram($str)) {
echo "String is a pangram.";
} else {
echo "String is not a pangram.";
}
?>
OutputString is a pangram.
Approach 3: Utilizing str_split() and count() Functions
Another approach involves splitting the string into an array of characters and then using count to determine if all letters are present.
PHP
<?php
function isPangram($str) {
// Convert the string to lowercase for
// case-insensitive comparison
$str = strtolower($str);
// Convert the string to an array
// of characters
$chars = str_split($str);
// Use count and array_unique to get
// unique letters
$uniqueLetters = count(array_unique(
array_filter($chars, 'ctype_alpha')
));
// Check if there are 26 unique letters
return $uniqueLetters == 26;
}
// Driver code
$str = "The quick brown fox jumps over the lazy dog";
if (isPangram($str)) {
echo "String is a pangram.";
} else {
echo "String is not a pangram.";
}
?>
OutputString is a pangram.
Approach 4: Using Bitwise Operations
In this approach, we use a single integer to track the presence of all 26 letters. Each bit in the integer represents whether a particular letter is present in the string or not.
Example:
PHP
<?php
function isPangram($str) {
// Convert the string to lowercase for
// case-insensitive comparison
$str = strtolower($str);
// Initialize a variable to track presence of letters
$checker = 0;
// Iterate through each character in
// the string
for ($i = 0; $i < strlen($str); $i++) {
$char = $str[$i];
// Check if the character is an
// alphabet letter
if (ctype_alpha($char)) {
// Find the position of the letter in the alphabet
$pos = ord($char) - ord('a');
// Set the corresponding bit to 1
$checker |= (1 << $pos);
}
}
// Check if all 26 bits are set
return $checker == (1 << 26) - 1;
}
// Driver code
$str = "The quick brown fox jumps over the lazy dog";
if (isPangram($str)) {
echo "String is a pangram.";
} else {
echo "String is not a pangram.";
}
?>
OutputString is a pangram.
Explore
Basics
Array
OOPs & Interfaces
MySQL Database
PHP Advance