How can a beginner effectively utilize loops in PHP to simplify the process of counting characters and assigning values based on specific criteria?

To simplify the process of counting characters and assigning values based on specific criteria in PHP, a beginner can effectively utilize loops. By iterating over the input string and checking each character against the specified criteria within the loop, the beginner can increment counters or assign values accordingly. This approach streamlines the process and makes it easier to manage complex tasks involving character manipulation.

$input_string = "Hello World";
$vowel_count = 0;
$consonant_count = 0;

foreach(str_split($input_string) as $char){
    if(preg_match('/[aeiou]/i', $char)){
        $vowel_count++;
    } else {
        $consonant_count++;
    }
}

echo "Vowel count: " . $vowel_count . "\n";
echo "Consonant count: " . $consonant_count;