php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #71057 var_export uses long array syntax, is not PSR-2 compliant
Submitted: 2015-12-08 11:20 UTC Modified: 2015-12-08 14:10 UTC
From: marcus at synchromedia dot co dot uk Assigned:
Status: Not a bug Package: Arrays related
PHP Version: 5.6.16 OS:
Private report: No CVE-ID: None
 [2015-12-08 11:20 UTC] marcus at synchromedia dot co dot uk
Description:
------------
Calling var_export with an array parameter generates PHP code that uses old-style long array syntax instead of PHP 5.4-style short array syntax.

There are also some spacing issues that mean that the generated code is not PSR-2 compliant (space after 'array' and 2-space indenting), and I don't see any good reason for that not to be the case.

It appears that this behaviour occurs in all versions since PHP 5.4, including 7.

Test script:
---------------
var_export(['a' => 0]);

Expected result:
----------------
[
    'a' => 0,
]

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


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2015-12-08 14:10 UTC] krakjoe@php.net
-Status: Open +Status: Not a bug
 [2015-12-08 14:10 UTC] krakjoe@php.net
The justification for short array syntax - that it is short - doesn't matter for generated code.

What matters is that the output is predictable across PHP versions, for that reason it doesn't make sense to change the format of the output in order to comply with a PSR that doesn't apply to internals, and was never meant too.

Definitely, not a bug ...
 [2016-02-10 13:48 UTC] shakaran at gmail dot com
Please, could we reconsider opening this bug?

Currently using var_export() for output arrays of arrays (for example doing translations) or big files with configs, it is very unreadable read a lot array() instead only []. And the syntax [] for arrays is specially usefull for non IT people because they understand pretty easily the keys and values for translations.

Some people is even hardcoding this behaviour with regex and json as you can see:

http://stackoverflow.com/questions/24316347/how-to-format-var-export-to-php5-4-array-syntax

I am specially interested for this for example:

https://github.com/barryvdh/laravel-translation-manager/issues/94#issuecomment-182273567

Probably this should be the default option, but could be implemented with a flag option parameter for new behaviour and make as default in upcoming versions since coulbe a BC if you change all the output in a lot php programs. At least the PHP user should could decide if he wants the new behaviour with a flag, and I think that it is pretty easy to implement that flag for output like that.
 [2019-05-21 20:33 UTC] vlad dot rmn at gmail dot com
With PHP 5 being no longer supported, perhaps it's time to actually change this.
 
PHP Copyright © 2001-2020 The PHP Group
All rights reserved.
Last updated: Sun Jan 26 05:01:24 2020 UTC