Skip to content

In a function, a foreach key bound to a static or global variable reads 0 inside the loop instead of the real key. #127

Closed

Description

@blacktrs

Summary

In a function, a foreach key bound to a static or global variable reads 0 inside the loop instead of the real key.

Repro

<?php
function st(array $a): void { static $k; foreach ($a as $k => $v) { echo var_export($k, true), ' '; } echo "\n"; }
function gl(array $a): void { global $gk; foreach ($a as $gk => $v) { echo var_export($gk, true), ' '; } echo "\n"; }
foreach ([[10, 20, 30], ['a' => 1, 'b' => 2], ['x' => 1, 5 => 2, 'y' => 3]] as $arr) { st($arr); gl($arr); }

Expected (php)

0 1 2 
0 1 2 
'a' 'b' 
'a' 'b' 
'x' 5 'y' 
'x' 5 'y' 

Actual (manticore 32f98675, Darwin arm64)

0.0 0.0 0.0 
0.0 0.0 0.0 
0.0 0.0 
0.0 0.0 
0.0 0.0 0.0 
0.0 0.0 0.0 

Tracking

Known-bug repro: tests/aot/repro/wrong/foreach_key_static_global_reads_zero.php — bash tests/aot/xfail.sh -k foreach_key_static_global_reads_zero reports XFAIL while this is open and XPASS once it is fixed; then move the repro into tests/aot/cases/ and close this issue.

Activity

  1. added
    bug
    wrong-answer
    on Oct 7, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugwrong-answer

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions