3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?php function setKey($session_keys, $value) { $current_key = ''; $counter = 0; $prev_step = $_SESSION; try { $session_keys_array = explode(':', $session_keys); krsort($session_keys_array); $c = count($session_keys_array); var_dump($c); $count = 0; foreach ($session_keys_array as $key) { $count++; $arr = array(); switch ($count) { case 1: arr[$key] = $value; break; case ($count > 1 AND $count < $c): $arr[$key] = $arr; break; case $c: $_SESSION[$key] = $arr; break; case ($count >$c): return $_SESSION[$key]; break; } } } catch (Exception $e) { //TODO:log notice if Session[currentKey] is not an array return false; } return array_key_exists($key,$prev_step) && eval('!empty($_SESSION' . $current_key . ');'); } $_SESSION['1']['2']['3'] = 'zzzz'; setKey('1:2:3', 77); echo $_SESSION['1']['2']['3'];
based on f2Kor
Output for 7.0.0 - 7.0.2
Fatal error: Cannot use temporary expression in write context in /in/teVZP on line 20
Process exited with code 255.
Output for 5.6.8 - 5.6.17
Parse error: syntax error, unexpected '=' in /in/teVZP on line 20
Process exited with code 255.
Output for 5.3.21 - 5.5.31
Parse error: syntax error, unexpected '[' in /in/teVZP on line 20
Process exited with code 255.