diff --git a/core/includes/install.inc b/core/includes/install.inc
index 4e70be4..e799001 100644
--- a/core/includes/install.inc
+++ b/core/includes/install.inc
@@ -209,6 +209,10 @@ function drupal_rewrite_settings($settings = array(), $settings_file = NULL) {
   }
   $contents = file_get_contents(DRUPAL_ROOT . '/' . $settings_file);
   if ($contents !== FALSE) {
+    // Append PHP tag if the settings.php file is empty.
+    if ($contents == '') {
+      $contents = "<?php\n";
+    }
     // Step through each token in settings.php and replace any variables that
     // are in the passed-in array.
     $buffer = '';
diff --git a/core/modules/system/lib/Drupal/system/Tests/System/SettingsRewriteTest.php b/core/modules/system/lib/Drupal/system/Tests/System/SettingsRewriteTest.php
index cd4af82..dbc6442 100644
--- a/core/modules/system/lib/Drupal/system/Tests/System/SettingsRewriteTest.php
+++ b/core/modules/system/lib/Drupal/system/Tests/System/SettingsRewriteTest.php
@@ -109,5 +109,21 @@ function testDrupalRewriteSettings() {
       drupal_rewrite_settings($test['settings'], $filename);
       $this->assertEqual(file_get_contents(DRUPAL_ROOT . '/' . $filename), "<?php\n" . $test['expected'] . "\n");
     }
+
+    // Test that <?php gets added to the start of an empty settings file.
+    $test = array(
+      'settings' => array(
+        'no_index_value_scalar' => (object) array(
+          'value' => FALSE,
+          'comment' => 'comment',
+          'required' => TRUE,
+        ),
+      ),
+      'expected' => '$no_index_value_scalar = false; // comment'
+    );
+    $filename = variable_get('file_public_path', conf_path() . '/files') . '/empty_settings.php';
+    file_put_contents(DRUPAL_ROOT . '/' . $filename, "");
+    drupal_rewrite_settings($test['settings'], $filename);
+    $this->assertEqual(file_get_contents(DRUPAL_ROOT . '/' . $filename), "<?php\n" . $test['expected'] . "\n");
   }
 }
