diff --git a/core/includes/install.inc b/core/includes/install.inc
index 879a6ae..1b3d9a3 100644
--- a/core/includes/install.inc
+++ b/core/includes/install.inc
@@ -208,6 +208,10 @@ function drupal_rewrite_settings($settings = array(), $settings_file = NULL) {
   }
   $contents = file_get_contents(DRUPAL_ROOT . '/' . $settings_file);
   if ($contents !== FALSE) {
+    // Initialize the contents for the settings.php file if it is empty.
+    if (trim($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 74c080d..40c394c 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,26 @@ 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.
+    // Set the array of settings that will be written to the file.
+    $test = array(
+      'settings' => array(
+        'no_index' => (object) array(
+          'value' => TRUE,
+          'required' => TRUE,
+        ),
+      ),
+      'expected' => '$no_index = true;'
+    );
+    // Make an empty file.
+    $filename = settings()->get('file_public_path', conf_path() . '/files') . '/mock_settings.php';
+    file_put_contents(DRUPAL_ROOT . '/' . $filename, "");
+
+    // Write the setting to the file.
+    drupal_rewrite_settings($test['settings'], $filename);
+
+    // Check that the result is just the php opening tag and the settings.
+    $this->assertEqual(file_get_contents(DRUPAL_ROOT . '/' . $filename), "<?php\n" . $test['expected'] . "\n");
   }
 }
