PHP Programs

Please click on 1-2 Ads to keep this website's Study Material FREE and regularly Updated.

Program 42: Write a program to count the total number of vowels and consonants in a string.

vowels_consonants.php

<!DOCTYPE html>  
<html> 
<head>	
	<title>PHP Programs TutorialDuniya.com</title>
</head>
<body>
	<?php
		$voCount = 0;  
		$coCount = 0;
		
		$str = "Good Morning! Mr. Abhishek Sharma. Had your breakfast? ";  
	
		for($i = 0; $i < strlen($str); $i++) 
		{   
			if( $str[$i] == 'a' || $str[$i] == 'e' || $str[$i] == 'i' || $str[$i] == 'o' || $str[$i] == 'u' || $str[$i] == 'A' || $str[$i] == 'E' || $str[$i] == 'I' || $str[$i] == 'O' || $str[$i] == 'U') 
			{  
				$voCount++;  
			}  
					
			else if(($str[$i] >= 'a' && $str[$i] <= 'z') || ($str[$i] >= 'A' && $str[$i] <= 'Z')) 
			{  
				$coCount++;  
			} 
		} 

		echo $str; 

		echo "Number of vowels : " , $voCount;  

		echo "\nNumber of consonants : " , $coCount;  
	?>  
</body>  
</html>



If you have any suggestion or found any error in the above program, then do let us know at [email protected] or just comment below.