php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #63784 var_export() produces EOL symbols irregardless of target OS
Submitted: 2012-12-16 23:25 UTC Modified: 2018-05-14 00:27 UTC
Votes:1
Avg. Score:1.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:0 (0.0%)
Same OS:0 (0.0%)
From: hayk at mail dot ru Assigned: mattficken (profile)
Status: Closed Package: *General Issues
PHP Version: 7.2.2 OS: Windows
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: hayk at mail dot ru
New email:
PHP Version: OS:

 

 [2012-12-16 23:25 UTC] hayk at mail dot ru
Description:
------------
var_export() produces EOL symbols irregardless of target OS. It's always "\n" even on Windows.

Test script:
---------------
<?php
	$a = array('a' => 1);
	echo var_export($a, true);
	echo strlen(var_export($a, true));
?>

Expected result:
----------------
array (
  'a' => 1,
)23

Actual result:
--------------
array (
  'a' => 1,
)21

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2012-12-23 18:36 UTC] felipe@php.net
Actually it is not only var_export(), var_dump() also uses hardcoded LF.
 [2018-03-13 01:09 UTC] mattficken@php.net
-Operating System: Windows XP +Operating System: Windows -PHP Version: 5.4.9 +PHP Version: 7.2.2
 [2018-03-13 01:09 UTC] mattficken@php.net
I can repro this on 7.2.2.

var_export() returns \n (10) before the ) character.

The doc for var_export() doesn't specify a \n or \r\n behavior. The doc does specify var_export() should return executable PHP code. Except for Windows Notepad, text editors, IDEs, etc... shouldn't have a problem with this. They can automatically handle both \r\n and \n in same document. So the code var_export() returns can be edited and executed in text editor, IDE, etc...

On Windows 10, the command prompt does handle \n as if it was \r\n. PHP_EOL on Windows does return \r\n to avoid causing problems for terminal emulation.

In userland, var_export() output is often not (only) dumped to terminal but may be used with string parsing, persistence, etc...  Switching to \r\n, because it would add an additional byte/char could break that. Or at least cause a slight performance problem.

It may technically be a bug. But given userland code and that Windows command prompt and text editors can handle \n instead of \r\n, I think this should be reclassified as a "Won't fix".

-Thoughts?


A more clear repro script:
<?php
	$a = array('a' => 1);
	echo var_export($a, true);
	echo strlen(var_export($a, true));
	
	$b = var_export($a, true);
	echo $b;
	echo strlen($b);
	
	function ord_array($str) {
		$ords = array();
	   
		for($i = 0; $i < strlen($str); $i++){       
			$s2 = substr($str,$i,1);
			$ords[] = ord($s2);
		}       
		return($ords);
	}

	echo PHP_EOL;

	echo var_dump(ord_array($b));
?>
 [2018-05-14 00:27 UTC] mattficken@php.net
-Status: Open +Status: Closed -Assigned To: +Assigned To: mattficken
 [2018-05-14 00:27 UTC] mattficken@php.net
see previous comment
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Apr 16 16:01:28 2024 UTC