What are the best practices for integrating Google Maps API V3 with PHP scripts for displaying polygon maps?

When integrating Google Maps API V3 with PHP scripts to display polygon maps, it is important to properly format the data for the polygons and then pass it to the JavaScript code that initializes the map. One way to achieve this is by using PHP to generate the JavaScript code dynamically with the polygon data included.

<?php
// Define polygon coordinates
$coordinates = array(
    array('lat' => 37.772, 'lng' => -122.214),
    array('lat' => 21.291, 'lng' => -157.821),
    array('lat' => -18.142, 'lng' => 178.431),
    array('lat' => -27.467, 'lng' => 153.027)
);

// Generate JavaScript code for polygon map
echo '<script>
    var map;
    function initMap() {
        map = new google.maps.Map(document.getElementById("map"), {
            center: {lat: 0, lng: 0},
            zoom: 2
        });

        var polygonCoords = [';
foreach ($coordinates as $coordinate) {
    echo '{lat: ' . $coordinate['lat'] . ', lng: ' . $coordinate['lng'] . '},';
}
echo '];

        var polygon = new google.maps.Polygon({
            paths: polygonCoords,
            strokeColor: "#FF0000",
            strokeOpacity: 0.8,
            strokeWeight: 2,
            fillColor: "#FF0000",
            fillOpacity: 0.35
        });
        polygon.setMap(map);
    }
</script>';
?>