php.net |  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
Description:
------------
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
---

https://github.com/php/php-src/blob/77118fc925b3e84be02a80d8da6bbbb47f8c37e1/main/php.h#L69

Var export definition
---

https://github.com/php/php-src/blob/8d3f8ca12a0b00f2a74a27424790222536235502/ext/standard/var.c#L437


See for example this line in the var_export definition: https://github.com/php/php-src/blob/8d3f8ca12a0b00f2a74a27424790222536235502/ext/standard/var.c#L495

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;


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

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] peehaa@php.net
-Status: Open +Status: Not a bug
 [2018-09-12 14:30 UTC] peehaa@php.net
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-2022 The PHP Group
All rights reserved.
Last updated: Mon Aug 15 19:05:44 2022 UTC