In PHP I have written a script to count the number of vowels in an array and output the result.
When running the script I get an Undefined offset
error.
What is wrong with this code?
for($a = 0; $a < count($expld); $a++) {
for($b = 0; $b < strlen($expld[$b]); $b++) {
if ($expld[$b] == 'A' || $expld[$b] == 'a' || $expld[$b] == 'E' || $expld[$b] == 'e'
|| $expld[$b] == 'I' || $expld[$b] == 'i' || $expld[$b] == 'O' || $expld[$b] == 'o'
|| $expld[$b] == 'U' || $expld[$b] == 'u')
{
$vowel++;
}
}
echo "$expld[$a] has $vowel vowels(s).<br> ";
}
It seems you quite messed up the indexing, check it out:
for($a = 0; $a < count($expld); $a++)
{
$vowel = 0;
for($b = 0; $b < strlen($expld[$a]); $b++)
{
if($expld[$a][$b] == 'A' || $expld[$a][$b] == 'a' || $expld[$a][$b] == 'E' || $expld[$a][$b] == 'e'
|| $expld[$a][$b] == 'I' || $expld[$a][$b] == 'i' || $expld[$a][$b] == 'O' || $expld[$a][$b] == 'o'
|| $expld[$a][$b] == 'U' || $expld[$a][$b] == 'u')
{
$vowel++;
}
}
echo "$expld[$a] has $vowel vowels(s).<br> ";
}
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments