PHP Program to Check if a Given String is Pangram or Not Last Updated : 08 Jul, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report 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.Table of ContentUsing an ArrayUsing SetUtilizing str_split() and count() FunctionsUsing Bitwise OperationsApproach 1: Using an ArrayThe 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 SetAn 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() FunctionsAnother 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 OperationsIn 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. Comment More infoAdvertise with us Next Article Javascript Program to Check if a given string is Pangram or not B blalverma92 Follow Improve Article Tags : PHP PHP-string Geeks Premier League 2023 Similar Reads Javascript Program to Check if a given string is Pangram or not In this article, we are going to implement algorithms to check whether a given string is a pangram or not. Pangram strings are those strings that contain all the English alphabets in them.Example: Input: âFive or six big jet planes zoomed quickly by tower.â Output: is a Pangram Explanation: Does'not 7 min read Check if a given String is Binary String or Not using PHP Given a String, the task is to check whether the given string is a binary string or not in PHP. A binary string is a string that should only contain the '0' and '1' characters. Examples:Input: str = "10111001"Output: The string is binary.Input: str = "123456"Output: The string is not binary.Table of 5 min read How to check if a String Contains a Substring in PHP ? Checking whether a string contains a specific substring is a common task in PHP. Whether you're parsing user input, filtering content, or building search functionality, substring checking plays a crucial role.MethodsBelow are the following methods by which we can check if a string contains a substri 1 min read How to check for empty string in PHP ? In this article, we will see how to check for empty string in PHP. String is a set of characters. A string is said to be empty, if it contains no characters. We can use empty() function to check whether a string is empty or not.here we have some common approachesTable of ContentUsing empty() functio 4 min read PHP to check substring in a string In this article, we will see how to check the substring in a string. We are given two strings & we have to check whether the second string is a substring of the first string or not using PHP built-in strpos() function. This function is case-sensitive, which means that it treats upper-case and lo 2 min read PHP program to check for Anagram An anagram is a word or phrase formed by rearranging the letters of another word or phrase, using all the original letters exactly once. To check for anagrams in PHP, remove spaces, convert to lowercase, sort characters, and compare the sorted strings or arrays.ExamplesInput : "anagram", "nagaram"Ou 5 min read Like