Challenge - Vowel Counter
Part of the Fundamentals section of Coddy's PHP journey — lesson 71 of 71.
Challenge
EasyRead one line of input:
- A string of text (e.g.,
Hello World)
Create a function called countVowels that accepts one parameter:
$textasstring
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" → 0Try 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
4Comparison & Logical Operators
Comparison OperatorsEquality & IdentityLogical Operators Part 1Logical Operators Part 2Recap - Simple Logic2Variables and Data Types
NumbersStrings and QuotesBooleansNaming ConventionsRecap - Variable InitEmpty VariablesString ConcatenationGetting User InputCast to Different Types5Conditional Logic
If StatementIf - ElseThe Ternary OperatorNull Coalescing OperatorSwitch StatementRecap - Making Decisions3Basic Operators
Arithmetic OperatorsModulo OperatorExponentiation OperatorCombined AssignmentIncrement/DecrementOperator PrecedenceRecap - Simple CalculationsString Operators