Menu
Coddy logo textTech

Challenge - Vowel Counter

Part of the Fundamentals section of Coddy's PHP journey — lesson 71 of 71.

challenge icon

Challenge

Easy

Read one line of input:

  • A string of text (e.g., Hello World)

Create a function called countVowels that accepts one parameter:

  • $text as string

The function should have a return type declaration of int and count the total number of vowels (a, e, i, o, u) in the string.

Use a for loop with strlen() to iterate through each character. For each character, use strtolower() to handle both uppercase and lowercase vowels, then check if it matches any vowel.

Call the function with the input value and print the returned result.

For example:

"Hello World" → 3 (e, o, o)
"AEIOU" → 5
"rhythm" → 0

Try it yourself

<?php
// Read input
$text = trim(fgets(STDIN));

// TODO: Create a function called countVowels that:
// - Accepts one parameter: $text as string
// - Has a return type declaration of int
// - Uses a for loop with strlen() to iterate through each character
// - Uses strtolower() to handle both uppercase and lowercase vowels
// - Counts and returns the total number of vowels (a, e, i, o, u)



// Call the function and print the result
echo countVowels($text);
?>

All lessons in Fundamentals