diff --git a/core/lib/Drupal/Core/Template/TwigTemplate.php b/core/lib/Drupal/Core/Template/TwigTemplate.php
index ee864ca..cb2d91d 100644
--- a/core/lib/Drupal/Core/Template/TwigTemplate.php
+++ b/core/lib/Drupal/Core/Template/TwigTemplate.php
@@ -41,8 +41,9 @@
    */
   final protected function getContextReference(&$context, $item)
   {
-    // Optimized version
-    if (!isset($context[$item])) {
+    // Optimized version. NULL is a valid value for $context[$item], we only
+    // want to error if it hasn't been defined at all.
+    if (!isset($context[$item]) && !array_key_exists($item, $context)) {
       // We don't want to throw an exception, but issue a warning instead.
       // This is the easiest way to do so.
       // @todo Decide based on prod vs. dev setting
diff --git a/core/modules/system/lib/Drupal/system/Tests/Theme/ThemeTestTwig.php b/core/modules/system/lib/Drupal/system/Tests/Theme/ThemeTestTwig.php
index c62f7c3..5ac99e0 100644
--- a/core/modules/system/lib/Drupal/system/Tests/Theme/ThemeTestTwig.php
+++ b/core/modules/system/lib/Drupal/system/Tests/Theme/ThemeTestTwig.php
@@ -2,7 +2,7 @@
 
 /**
  * @file
- * Definition of Drupal\system\Tests\Theme\ThemeTest.
+ * Contains \Drupal\system\Tests\Theme\ThemeTestTwig.
  */
 
 namespace Drupal\system\Tests\Theme;
@@ -10,7 +10,7 @@
 use Drupal\simpletest\WebTestBase;
 
 /**
- * Tests low-level theme functions.
+ * Tests theme functions with the Twig engine.
  */
 class ThemeTestTwig extends WebTestBase {
 
@@ -19,7 +19,7 @@ class ThemeTestTwig extends WebTestBase {
    *
    * @var array
    */
-  public static $modules = array('theme_test');
+  public static $modules = array('theme_test', 'twig_theme_test');
 
   public static function getInfo() {
     return array(
@@ -69,4 +69,18 @@ function testFindThemeTemplates() {
     $templates = drupal_find_theme_templates($cache, '.html.twig', drupal_get_path('theme', 'test_theme_twig'));
     $this->assertEqual($templates['node__1']['template'], 'node--1', 'Template node--1.html.twig was found in test_theme_twig.');
   }
+
+  /**
+   * Tests that the Twig engine handles PHP data correctly.
+   */
+  function testTwigVariableDataTypes() {
+    config('system.theme')
+      ->set('default', 'test_theme_twig')
+      ->save();
+    $this->drupalGet('twig-theme-test/php-variables');
+    foreach (_test_theme_twig_php_values() as $type => $value) {
+      $this->assertRaw('<li>' . $type . ': ' . $value['expected'] . '</li>');
+    }
+  }
+
 }
diff --git a/core/modules/system/tests/modules/twig_theme_test/lib/Drupal/twig_theme_test/TwigThemeTestController.php b/core/modules/system/tests/modules/twig_theme_test/lib/Drupal/twig_theme_test/TwigThemeTestController.php
new file mode 100644
index 0000000..1657fe1
--- /dev/null
+++ b/core/modules/system/tests/modules/twig_theme_test/lib/Drupal/twig_theme_test/TwigThemeTestController.php
@@ -0,0 +1,32 @@
+<?php
+/**
+ * @file
+ * Contains \Drupal\twig_theme_test\TwigThemeTestController.
+ */
+
+namespace Drupal\twig_theme_test;
+
+use Drupal\Core\ControllerInterface;
+use Symfony\Component\DependencyInjection\ContainerInterface;
+
+
+/**
+ * Controller routines for book routes.
+ */
+class TwigThemeTestController implements ControllerInterface {
+
+  /**
+   * Creates the controller.
+   */
+  public static function create(ContainerInterface $container) {
+    return new static();
+  }
+
+  /**
+   * Menu callback for testing php variables in a Twig template.
+   */
+  public function phpVariablesRender() {
+    return theme('twig_theme_test_php_variables');
+  }
+
+}
diff --git a/core/modules/system/tests/modules/twig_theme_test/templates/twig_theme_test.php_variables.html.twig b/core/modules/system/tests/modules/twig_theme_test/templates/twig_theme_test.php_variables.html.twig
new file mode 100644
index 0000000..9035e35
--- /dev/null
+++ b/core/modules/system/tests/modules/twig_theme_test/templates/twig_theme_test.php_variables.html.twig
@@ -0,0 +1,6 @@
+{# Output for Twig Theme PHP variables test. #}
+<ul>
+{% for key, php_value in php_values %}
+  <li>{{ key }}: {{ php_value.value }}</li>
+{% endfor %}
+</ul>
diff --git a/core/modules/system/tests/modules/twig_theme_test/twig_theme_test.info.yml b/core/modules/system/tests/modules/twig_theme_test/twig_theme_test.info.yml
new file mode 100644
index 0000000..ecc446f
--- /dev/null
+++ b/core/modules/system/tests/modules/twig_theme_test/twig_theme_test.info.yml
@@ -0,0 +1,6 @@
+name: 'Twig theme test'
+description: 'Support module for Twig theme system testing.'
+package: Testing
+version: VERSION
+core: 8.x
+hidden: true
diff --git a/core/modules/system/tests/modules/twig_theme_test/twig_theme_test.module b/core/modules/system/tests/modules/twig_theme_test/twig_theme_test.module
new file mode 100644
index 0000000..2ca2cd0
--- /dev/null
+++ b/core/modules/system/tests/modules/twig_theme_test/twig_theme_test.module
@@ -0,0 +1,50 @@
+<?php
+
+/**
+ * Implements hook_theme().
+ */
+function twig_theme_test_theme($existing, $type, $theme, $path) {
+  $items['twig_theme_test_php_variables'] = array(
+    'template' => 'twig_theme_test.php_variables',
+  );
+  return $items;
+}
+
+/*
+ * Helper function to test PHP variables in the Twig engine.
+ */
+function _test_theme_twig_php_values() {
+  // Prefix each variable with "twig_" so that Twig doesn't get confused
+  // between a variable and a primitive. Arrays are not tested since they should
+  // be a Drupal render array.
+  return array(
+    'twig_null' => array(
+      'value' => NULL,
+      'expected' => '',
+    ),
+    'twig_bool_false' => array(
+      'value' => FALSE,
+      'expected' => '',
+    ),
+    'twig_bool_true' => array(
+      'value' => TRUE,
+      'expected' => '1',
+    ),
+    'twig_int' => array(
+      'value' => 1,
+      'expected' => '1',
+    ),
+    'twig_int_0' => array(
+      'value' => 0,
+      'expected' => '0',
+    ),
+    'twig_float' => array(
+      'value' => 122.34343,
+      'expected' => '122.34343',
+    ),
+    'twig_string' => array(
+      'value' => 'Hello world!',
+      'expected' => 'Hello world!',
+    ),
+  );
+}
diff --git a/core/modules/system/tests/modules/twig_theme_test/twig_theme_test.routing.yml b/core/modules/system/tests/modules/twig_theme_test/twig_theme_test.routing.yml
new file mode 100644
index 0000000..cdc0ac1
--- /dev/null
+++ b/core/modules/system/tests/modules/twig_theme_test/twig_theme_test.routing.yml
@@ -0,0 +1,6 @@
+twig_theme_test_php_variables:
+  pattern: '/twig-theme-test/php-variables'
+  defaults:
+    _content: '\Drupal\twig_theme_test\TwigThemeTestController::phpVariablesRender'
+  requirements:
+    _permission: 'access content'
diff --git a/core/modules/system/tests/themes/test_theme_twig/test_theme_twig.theme b/core/modules/system/tests/themes/test_theme_twig/test_theme_twig.theme
new file mode 100644
index 0000000..43aac12
--- /dev/null
+++ b/core/modules/system/tests/themes/test_theme_twig/test_theme_twig.theme
@@ -0,0 +1,13 @@
+<?php
+
+/**
+ * @file
+ * Theme to help test the Twig engine.
+ */
+
+/**
+ * Implements THEME_preprocess_twig_theme_test_php_variables().
+ */
+function test_theme_twig_preprocess_twig_theme_test_php_variables(&$variables) {
+  $variables['php_values'] = _test_theme_twig_php_values();
+}
diff --git a/core/themes/engines/twig/twig.engine b/core/themes/engines/twig/twig.engine
index 6166da9..1ba60a5 100644
--- a/core/themes/engines/twig/twig.engine
+++ b/core/themes/engines/twig/twig.engine
@@ -98,6 +98,11 @@ function twig_render_var($arg) {
     $arg = &$arg->getReference();
   }
 
+  // Check for numeric zero.
+  if ($arg === 0) {
+    return 0;
+  }
+
   // == is true also for empty arrays
   if ($arg == NULL) {
     return NULL;
