Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[Expression Language] Null coalescing operator doesn't work? #54359

@adamkiss

Description

@adamkiss

Symfony version(s) affected

7.0

Description

According to docs, we can use null-coalescing operator to provide fallback to potentially empty or even nonexistent variables. However I can't make it work, even in a simple file with nothing but Expression Language.

How to reproduce

$lang = new ExpressionLanguage();

$data = [
    'exists' => true,
];

$expression1 = 'notexists ?? "nope,"';
$expression2 = '(notexists ?? "nope,")';
$expression3 = '(notexists ?? false) ? "yes" : "no"';

foreach ([$expression1, $expression2, $expression3] as $exp) {
    try {
        var_dump($lang->evaluate($exp, $data));
    } catch (\Throwable $th) {
        echo('!');
    }
}
// expected: nope,nope,no
// actual: !!!

(In the reproduction I have suppressed the throws, but every evaluate fails with Fatal error: Uncaught Symfony\Component\ExpressionLanguage\SyntaxError: Variable "notexists" is not valid around position …

Possible Solution

No response

Additional Context

No response

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