diff --git a/core/lib/Drupal/Core/Cache/PhpBackend.php b/core/lib/Drupal/Core/Cache/PhpBackend.php
index 23ed324..a9e9338 100644
--- a/core/lib/Drupal/Core/Cache/PhpBackend.php
+++ b/core/lib/Drupal/Core/Cache/PhpBackend.php
@@ -263,9 +263,18 @@ public function removeBin() {
    */
   protected function writeItem($cid, \stdClass $item) {
     $data = str_replace('\\', '\\\\', serialize($item));
-    $content = "<?php return unserialize(<<<EOF
+    $EOF = 'EOF';
+    $suffix = mt_rand(0, 100000);
+    $iterations = 0;
+    while (strpos($data, "\n$EOF\n") !== FALSE) {
+      if ($iterations++ == 100) {
+        return FALSE;
+      }
+      $EOF .= $suffix++;
+    }
+    $content = "<?php return unserialize(<<<$EOF
 $data
-EOF
+$EOF
 );";
     $this->storage()->save($cid, $content);
   }
diff --git a/core/modules/system/lib/Drupal/system/Tests/Cache/GenericCacheBackendUnitTestBase.php b/core/modules/system/lib/Drupal/system/Tests/Cache/GenericCacheBackendUnitTestBase.php
index b390429..abf6958 100644
--- a/core/modules/system/lib/Drupal/system/Tests/Cache/GenericCacheBackendUnitTestBase.php
+++ b/core/modules/system/lib/Drupal/system/Tests/Cache/GenericCacheBackendUnitTestBase.php
@@ -161,6 +161,16 @@ public function testSetGet() {
     $this->assertFalse($cached->valid, 'Item is marked as valid.');
     $this->assertEqual($cached->created, REQUEST_TIME, 'Created time is correct.');
     $this->assertEqual($cached->expire, REQUEST_TIME - 3, 'Expire time is correct.');
+
+    $this->assertIdentical(FALSE, $backend->get('test4'), "Backend does not contain data for cache id test4.");
+    $with_eof = array('foo' => "\nEOF\nd");
+    $backend->set('test4', $with_eof);
+    $cached = $backend->get('test4');
+    $this->assert(is_object($cached), "Backend returned an object for cache id test4.");
+    $this->assertIdentical($with_eof, $cached->data);
+    $this->assertTrue($cached->valid, 'Item is marked as valid.');
+    $this->assertEqual($cached->created, REQUEST_TIME, 'Created time is correct.');
+    $this->assertEqual($cached->expire, Cache::PERMANENT, 'Expire time is correct.');
   }
 
   /**
