php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #76811 Add option to var_export so arrays use brackets []
Submitted: 2018-08-29 20:31 UTC Modified: 2021-08-23 14:50 UTC
Votes:3
Avg. Score:3.3 ± 0.5
Reproduced:1 of 1 (100.0%)
Same Version:0 (0.0%)
Same OS:0 (0.0%)
From: mark at seventhcycle dot net Assigned:
Status: Open Package: Variables related
PHP Version: Next Minor Version OS: Any
Private report: No CVE-ID: None
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please — but make sure to vote on the bug!
Your email address:
MUST BE VALID
Solve the problem:
46 - 9 = ?
Subscribe to this entry?

 
 [2018-08-29 20:31 UTC] mark at seventhcycle dot net
Description:
------------
Right now, any arrays that are exported via var_export use the longer array() syntax.

What would be useful here is a third optional argument to var_export:

e.g: var_export($arr, FALSE, TRUE);

Where if the third argument is set to true, the output would use the shorter, PHP 5.4 syntax.

There are already functions out there (search for var_export54) that do this sort of thing, however, it would be useful if this were in core PHP.

The third argument isn't necessary, however, I propose doing it this way to not break compatibility for existing people who are using var_export.


Test script:
---------------
$arr = [
    'key' => 'value',
    'mushroom' => [
        'badger' => 1
    ]
];

var_export($arr, FALSE, TRUE);

Expected result:
----------------
[
    'key' => 'value',
    'mushroom' => [
        'badger' => 1
    ]
]


Actual result:
--------------
array (
  'key' => 'value',
  'mushroom' => 
  array (
    'badger' => 1,
  ),
)

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2018-08-29 22:38 UTC] cmb@php.net
Makes some sense, in my opinion.  Instead of bool parameter, I'd
go for a bitset instead, since there may well be other useful
options.  I suggest to discuss this on the internals@ mailing
list[1].

[1] <http://php.net/mailing-lists.php#internals>
 [2021-08-23 14:50 UTC] cmb@php.net
-Package: Arrays related +Package: Variables related
 [2021-08-23 14:50 UTC] cmb@php.net
There is a respective RFC, but apparently that went nowhere.

[1] <https://wiki.php.net/rfc/var-export-array-syntax>
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Apr 19 04:01:28 2024 UTC