Count Vowels (PHP)
Write a PHP function that counts the number of vowel characters in a given string. Vowels are a, e, i, o, u (both lowercase and uppercase). The function should return the total vowel count.
You can use a loop to iterate through each character and check if it belongs to the vowel set. PHP strings can be accessed like arrays using bracket notation or
substr()
. Use
strlen()
to get the string length and
in_array()
or a string comparison to check for vowels.
This problem introduces PHP string manipulation with loops. The function takes a string parameter and returns an integer. Edge cases include empty strings (return 0), strings with no vowels (return 0), and mixed case.
The vowel counting problem introduces PHP string manipulation with loops and arrays. The
in_array()
function is used to check if a character belongs to the vowel set. PHP strings can be accessed like character arrays using bracket notation.
The vowel counting problem is a great way to learn PHP string handling. PHP treats strings as sequences of bytes that can be accessed via array indices. Using
strlen()
to get the length and
in_array()
on individual characters demonstrates core PHP array functions. Edge cases include empty strings, strings with no vowels, and mixed case. Returning an integer count is the expected output type in PHP.
Example Input & Output
All 10 vowels.
No vowels.
3 vowels: e,o,o.
Empty string.
5 vowels: o,a,i,u,i.
Algorithm Flow

Solution Approach
Create an array containing all vowel characters: both lowercase a, e, i, o, u and uppercase A, E, I, O, U. Initialize a counter variable to 0. Loop through each character of the input string using a for loop with an index variable from 0 up to strlen($text) - 1.
PHP strings can be accessed as character arrays using bracket notation like $text[$i]. For each character, check if it exists in the vowels array using in_array(). If it matches, increment the counter. After the loop completes, return the final count.
An alternative approach converts the entire string to lowercase first using strtolower(), then checks only against lowercase vowels. This reduces the vowel array to 5 elements and simplifies the comparison. The strlen() function works correctly for ASCII strings, returning the number of bytes.
Edge cases include empty strings (the loop doesn't execute, counter stays 0), strings with no vowels (same result), and strings with all vowels (counter equals length). The function handles mixed case correctly since both uppercase and lowercase variants are in the vowel array.
The time complexity is O(n) where n is the string length. The space complexity is O(1) since the vowel lookup array has a fixed size of 10 characters regardless of input size.
Best Answers
<?php
function countVowels($text) {
$vowels = ['a','e','i','o','u','A','E','I','O','U'];
$count = 0;
for ($i = 0; $i < strlen($text); $i++) {
if (in_array($text[$i], $vowels)) $count++;
}
return $count;
}Comments (0)
Join the Discussion
Share your thoughts, ask questions, or help others with this Challenge.
