How to var_export export export array in short syntax

Asked

Viewed 64 times

2

According to the PHP Handbook, about the function var_export

var_export() gets structured information about a given variable. It is similar to var_dump() with one exception: the returned representation is a valid PHP code.

That is, it should return a valid PHP code for a given value passed by parameter.

The problem is that in PHP 5.4, the settings for a array have changed. You no longer need to use the keyword array, but only use brackets.

Example:

// Versões anteriores ao PHP 5.4
$a = array(1, 2, 3);
// Versões iguais ou posteriores ao PHP 5.4
$a = [1, 2, 3]

When I do the var_export in that same array (even in PHP 5.4), it returns this to me:

array ( 0 => 1, 1 => 2, 2 => 3, )

I would like the var_export returns the array this way:

[ 0 => 1, 1 => 2, 2 => 3, ]

Is there any way to resolve this situation?

PHP has already fixed this in newer versions?

  • Why don’t you use the var_dump() even?

1 answer

3


The var_export continue exporting variables in format array() in latest versions.

Although PHP 5.4 introduces the simplified syntax of bracketed arrays [ ], the use of arrays in the format array() keeps working smoothly.

Change the output of var_export for short syntax would only bring more problems for those using older versions and is a superfluous change, since the common syntax still works array().

Maybe in the future when array() no longer exists this is changed.

  • I think a regular expression to solve this would be the solution then! However, it would be very complex!

  • @Wallacemaxters but convert to what? Any specific reason? array() still works normally

  • is because I really want to :) I wish I could export the code and have it with the brackets.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.