Bug #76873 [Opn->Nab]: var_export does not use PHP_EOL, always prints \n (a LF)
| From: | peehaa@php.net | Date: | Wed, 12 Sep 2018 14:30:54 +0000 |
| Subject: | Bug #76873 [Opn->Nab]: var_export does not use PHP_EOL, always prints \n (a LF) | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-217018@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=76873&edit=1
ID: 76873
Updated by: peehaa@php.net
Reported by: martijn at gripp dot com
Summary: var_export does not use PHP_EOL, always prints \n (a
LF)
-Status: Open
+Status: Not a bug
Type: Bug
Package: Output Control
Operating System: Windows
PHP Version: 7.0.31
Block user comment: N
Private report: N
New Comment:
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
Previous Comments:
------------------------------------------------------------------------
[2018-09-12 14:21:37] 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:19:32] 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;
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=76873&edit=1