Jason Stege has created a KML object class for PHP5 that speeds up the creation of KML files, keeps the code tidy, and also lets you zip the result into a KMZ file. If you code in PHP, you definitely want to take a look at his work, which is freely available for download on his blog.