Skip to content

ternary reference assignment should be legal #18043

@divinity76

Description

@divinity76

Description

I wanted to do

foreach((is_array($value) ? $value['images'] : $value->images) as &$image) {
// process $image
}

but that silently fail (!) because foreach will create an internal copy and iterate the copy (see `&$image);
So instead I tried:

$iter = is_array($value) ? (&$value['images']) : (&$value->images);
foreach($iter as &$image) {
// process $image
}

but that fail because ternary reference assignment is illegal (?)
So instead I have to do

if(is_array($value)){
    $iter = &$value['images'];
} else {
    $iter = &$value->images;
}
foreach($iter as &$image) {
// process $image
}

which works.
Now it feels like I'm fighting the language itself.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions