PHP Program to Count Vowels in a String

PHP

A string is a sequence of characters, including alphabets, numbers, and symbols. In this tutorial, we are going to learn how we can count the number of vowels in a given string in PHP using different approaches. Vowels in English are a, e, i, o, u, and they can be either uppercase or lowercase.

What is a Vowel?

Vowels are alphabetic characters that represent specific speech sounds. There are a total of five vowels, both uppercase and lowercase in the English language:

a, e, i, o, u
A, E, I, O, U

Example 1

  • Input: string = "Tutorialspoint"
  • Output: 6

Explanation

The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. Which are a total of 6 vowels.

Example 2

  • Input: string = "PHP"
  • Output: 0

Explanation

The string "PHP" does not contain any vowels, so the count is 0.

Example 3

  • Input: string = "Ayush Mishra"
  • Output: 4

Explanation

The vowels in the string are A, u, i, a. There are a total of 4 vowels.

Below are different approaches to counting vowels in a string using PHP

  • Using Direct Logic Approach
  • Using a Function

Count Vowels in a String Using Direct Logic Approach

We use a simple loop to traverse the string and check if each character is a vowel. If it is, we increment the counter. After the loop ends, we return the total count of vowels ?

Steps for Implementation

  1. First, define a string as the input parameter.
  2. Now, initialize a count variable to 0.
  3. Loop through each character in the string.
  4. For each character, check if it is a vowel (case-insensitive).
  5. Increase the count variable for each vowel.
  6. Return the count of vowels.

Implementation Code

<?php

$string = "Anshu Ayush";
$vowel_count = 0;

// Convert string to lowercase
$string_lower = strtolower($string);

// Loop through each character in the string
for ($i = 0; $i < strlen($string_lower); $i++) {
    if (in_array($string_lower[$i], ['a', 'e', 'i', 'o', 'u'])) {
        $vowel_count++;
    }
}

// Output
echo "The number of vowels in the string '$string' is: $vowel_count";

?>
The number of vowels in the string 'Anshu Ayush' is: 4

Time Complexity: O(n)
Space Complexity: O(1)

Count Vowels in a String Using Function

In this approach, we use a function to count the number of vowels in a string. We use function so that we can later use function whenever necessary ?

Steps for Implementation

  1. First, create a function that accepts a string as an input.
  2. Now, initialize a count variable to 0 and convert the string to lowercase.
  3. Traverse the string and check for vowels using a predefined list.
  4. Increase the count variable for each vowel.
  5. Return the count of vowels.

Implementation Code

<?php
// Function to count vowels
function count_vowels($string) {
    $vowel_count = 0;
    $string = strtolower($string); 
    
    for ($i = 0; $i < strlen($string); $i++) {
        if (in_array($string[$i], ['a', 'e', 'i', 'o', 'u'])) {
            $vowel_count++;
        }
    }
    return $vowel_count;
}

$string = "Anshu Ayush";
$vowel_count = count_vowels($string);

echo "The number of vowels in the string '$string' is: $vowel_count";
?>
The number of vowels in the string 'Anshu Ayush' is: 4

Time Complexity: O(n)
Space Complexity: O(1)

Conclusion

Both approaches provide efficient solutions for counting vowels in a string with O(n) time complexity. The function-based approach offers better code reusability and modularity for larger applications.

Updated on: 2026-03-15T10:44:12+05:30

8K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements