How can the logical error in the code snippet be corrected to achieve the desired output format?

The issue in the code snippet is that the concatenation operator "." is being used incorrectly to append elements to the $output string. To correct this, we should use the correct syntax for concatenation in PHP, which is the dot-equals operator ".=". This operator appends the right-hand side to the left-hand side variable. By using ".=" instead of ".", we can achieve the desired output format.

<?php
$colors = array("red", "green", "blue", "yellow");
$output = "";

foreach ($colors as $color) {
    $output .= $color . ", ";
}

// Remove the last comma and space
$output = rtrim($output, ", ");

echo $output;
?>