|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #76873 var_export does not use PHP_EOL, always prints \n (a LF)
Submitted: 2018-09-12 14:19 UTC Modified: 2018-09-12 14:30 UTC
From: martijn at gripp dot com Assigned:
Status: Not a bug Package: Output Control
PHP Version: 7.0.31 OS: Windows
Private report: No CVE-ID: None
 [2018-09-12 14:19 UTC] martijn at gripp dot com
The var_export function should respect your operating system's line endings. In windows, line endings are CLRF, but var_export always outputs RF only, (a '\n').

The PHP_EOL global contains '\n' or '\r\n', on Unix and windows respectively, and the var_export function should use the PHP_EOL instead of the hardcoded '\n'.

Relevant files:

EOL Definition

Var export definition

See for example this line in the var_export definition:

The '\n' is hardcoded.

This is not a problem on Linux because on Linux '\n' already is the correct line-feed return.

Test script:
$example = var_export([], true);

// run this on Windows
$clrfMatch = preg_match("/(\r\n)/", $example); // should be true
$lfMatch = preg_match("/(\n)/", $example); // should be false

echo $clrfMatch ? 'true' : 'false'; echo "  // should be true" . \PHP_EOL;
echo $lfMatch ? 'true': 'false'; echo "  // should be false" . \PHP_EOL;


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2018-09-12 14:21 UTC] martijn at gripp dot com
I forgot to mention, this bug is annoying because we use a git version control where files that are generated by a php script that uses var_export are stored. Every single time the generator runs, many of the files get '\n's where there were previously '\r\n's
 [2018-09-12 14:30 UTC]
-Status: Open +Status: Not a bug
 [2018-09-12 14:30 UTC]
This is not a bug, if you would like to see this feature open a feature request for it.

But before you do note:

- doing PHP_EOL by default breaks the current interoperability from e.g. windows -> nix
- IDEs work with both just fine on Windows
- There is the core.autocrlf git setting that fixes your actual problem I assume you are having
PHP Copyright © 2001-2023 The PHP Group
All rights reserved.
Last updated: Mon Mar 27 11:03:36 2023 UTC