Skip to content

var_export cannot process "<" some parts of stringed key names #18308

@WebbKodsFrilansaren

Description

@WebbKodsFrilansaren

Description

I was writing a simple parser for turning var_exported PHP arrays() into [] PHP arrays and while I was experimenting in handling special edge-cases, using PHP 8.2.0 (cli) (built: Dec 6 2022 15:31:23) (ZTS Visual C++ 2019 x64), the following code:

<?php
$array = ['>GE<T' => ['users' => ['|' => [], ':' => ['id' => [],],], 'test' => [], 'about' => [],], 'POST' => ['users' => ['|' => [], ':' => ['id' => [],],],],];

echo var_export($array) . "<br>";

Resulted in this output:

array ( '>GE array ( 'users' => array ( '|' => array ( ), ':' => array ( 'id' => array ( ), ), ), 'test' => array ( ), 'about' => array ( ), ), 'POST' => array ( 'users' => array ( '|' => array ( ), ':' => array ( 'id' => array ( ), ), ), ), )

But I expected this output instead:

array ( '>GE<T' => array ( 'users' => array ( '|' => array ( ), ':' => array ( 'id' => array ( ), ), ), 'test' => array ( ), 'about' => array ( ), ), 'POST' => array ( 'users' => array ( '|' => array ( ), ':' => array ( 'id' => array ( ), ), ), ), )

However, this works just fine:

<?php
$array = ['>GET<' => ['users' => ['|' => [], ':' => ['id' => [],],], 'test' => [], 'about' => [],], 'POST' => ['users' => ['|' => [], ':' => ['id' => [],],],],];

echo var_export($array) . "<br>";

RESULT: array ( '>GET<' => array ( 'users' => array ( '|' => array ( ), ':' => array ( 'id' => array ( ), ), ), 'test' => array ( ), 'about' => array ( ), ), 'POST' => array ( 'users' => array ( '|' => array ( ), ':' => array ( 'id' => array ( ), ), ), ), )

If the stringed key begins with "<" it isn't parsed correctly by var_export, but any typical valid character(s) that come before it works. More examples (THIS is the variable going in, and BECOMES is the corresponding var_export() result):

THIS: ['<GET' => ['users' => 
BECOMES: array ( ' array ( 'users' =>

THIS: ['G<ET' => ['users' =>
BECOMES: array ( 'G array ( 'users' =>

THIS: ['GE<T' => ['users' =>
BECOMES: array ( 'GE array ( 'users' =>

THIS: ['GET<' => ['users' =>
BECOMES: array ( 'GET<' => array ( 'users' =>

All seems ok using ">":
THIS: ['>GET' => ['users' =>
BECOMES: array ( '>GET' => array ( 'users' =>

THIS: ['G>ET' => ['users' =>
BECOMES: array ( 'G>ET' => array ( 'users' =>

THIS: ['GE>T' => ['users' =>
BECOMES: array ( 'GE>T' => array ( 'users' =>

THIS: ['GET>' => ['users' =>
BECOMES: array ( 'GET>' => array ( 'users' =>

Once again; "THIS" is just part of a otherwise valid PHP array ($array) and "BECOMES" is how echo var_export($array) . "<br>";

Btw, this is my very first issue report published on GitHub! ^_^

Thanks in advance I love using PHP otherwise! <3 🫡

Best regards from Sweden,
WebbKodsFrilansaren ("WKF").

PHP Version

PS C:\xampp\htdocs> php -v
PHP 8.2.0 (cli) (built: Dec 6 2022 15:31:23) (ZTS Visual C++ 2019 x64)
Copyright (c) The PHP Group
Zend Engine v4.2.0, Copyright (c) Zend Technologies

Operating System

Windows 10 64-bit

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions