Create a PHP function to count the number of vowels in a string.

<?php
function countVowels($str) {
    $vowels = ['a', 'e', 'i', 'o', 'u'];
    $str = strtolower($str);
    return count(array_intersect(str_split($str), $vowels));
}

// Example usage:
$count = countVowels('Hello, World!');
echo "Number of vowels: $count";
?>

 

Post your Answer