An *empty* PHP file "<?php", when included, doesn't get parsed and is added to Output Buffer instead, why so?

StackOverflow https://stackoverflow.com/questions/22958155

Question

The simplest way to replicate is through a web context, by doing:

  1. Create a file.php with the following contents: <?php. No whitespace!

  2. Make index.php with the following contents:

    <?php
    
    header('Content-Type: text/plain');
    
    require 'file.php';
    
    echo 'test';
    

Now, witness the results.

Expected: test

Actual: <?phptest

I've stumbled upon this quite some times while developing, and it's quite annoying.

Environment:

  • Linux 3.13.7-1-ARCH #1 SMP PREEMPT Mon Mar 24 20:06:08 CET 2014 x86_64
  • Apache/2.4.9 (Unix) (built: Mar 16 2014 12:48:23)
  • PHP 5.5.10 (cli) (built: Mar 5 2014 17:41:10)

Why such behavior, how come PHP doesn't parse this?

Was it helpful?

Solution

Quoting from the docs:

Note:

In PHP 5.2 and earlier, the parser does not allow the <?php opening tag to be the only thing in a file. This is allowed as of PHP 5.3 provided there are one or more whitespace characters after the opening tag.

my emphasis

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top