PHP in HindiProgramming Language

How to Count Vowels in a String Using PHP?

Problem:- How to Count Vowels in a String Using PHP?

Solution – Count Vowels in a String Using PHP

<?php
$string = 'Hello User! I Am a PHP Script';
$vowels = 0;
for($i = 0, $j = strlen($string) ; $i< $j ; $i++){
	if(strstr('aeiouAEIOU', $string[$i])){
		$vowels++;
	}
}
echo $vowels;
?>

How it Works?

The function strlen() used to total length for the loop and strstr() is used to check the single string is available in main string or not. if yes then we increase $vowels value and finally we will get 8 vowels in $string.

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button
Close