If you want a simpler way to get around the <script> tag problem try:
<?php
$script = $doc->createElement ('script');\
// Creating an empty text node forces <script></script>
$script->appendChild ($doc->createTextNode (''));
$head->appendChild ($script);
?>