php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #66312 PHP code inside <script> deletes LF
Submitted: 2013-12-17 23:19 UTC Modified: 2013-12-17 23:26 UTC
From: danielslcosta at gmail dot com Assigned:
Status: Not a bug Package: *General Issues
PHP Version: 5.5.7 OS: Windows 7
Private report: No CVE-ID: None
View Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: danielslcosta at gmail dot com
New email:
PHP Version: OS:

 

 [2013-12-17 23:19 UTC] danielslcosta at gmail dot com
Description:
------------
To prevent some editors showing errors in Javascript code with PHP code inside it, I comment the lines with PHP code. 

When you run the code, the alert and </script> goes one line up, and become commented. It seems it's "eating" the Line Feeds.

Tested with LF and CRLF, both in Windows 7/PHP 5.5.7 TS and CentOS 6.4 with PHP 5.5.5.

If you put a space or any other character after the closing tag ?> it works.

Test script:
---------------
<script type='text/javascript'>
    //<?php if (1) { ?>
    alert('test');
    //<?php } ?>
</script>

<script type='text/javascript'>
    //<?php if (1) { ?>/
    alert('test');
    //<?php } ?>/
</script>

Expected result:
----------------
The JS alert box should pop and the resulting HTML should be:

<script type='text/javascript'>
    //
    alert('test');
    //
</script>

<script type='text/javascript'>
    ///
    alert('test');
    ///
</script>

Actual result:
--------------
The 'alert' and '</script>' are commented

<script type='text/javascript'>
    //    alert('test');
    //</script>

<script type='text/javascript'>
    ///
    alert('test');
    ///
</script>

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2013-12-17 23:26 UTC] rasmus@php.net
-Status: Open +Status: Not a bug
 [2013-12-17 23:26 UTC] rasmus@php.net
This is on purpose to avoid stray newlines following closing PHP tags that might force out response headers prematurely which in turn means a subsequent header()  call won't work. Not ideal, we know, but changing this would break a lot of legacy code. You need to put 2 CRs there if you want to force one or use ?><space><cr>
 [2013-12-17 23:36 UTC] danielslcosta at gmail dot com
Ok, just found out the FAQ about this and the original discussion about it back in 1998. 

Thanks anyway - been a PHP programmer for 5+ years and never noticed this.
 
PHP Copyright © 2001-2026 The PHP Group
All rights reserved.
Last updated: Thu Oct 08 15:00:02 2026 UTC