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); foreach ($session_keys_array as $key) { $counter++; $current_key .= '[\'' . $key . '\']'; if (count($session_keys_array) > $counter) { if (is_array($prev_step) && !array_key_exists($key, $prev_step)) { eval('$_SESSION' . $current_key . ' = array();'); } else if (!is_array($prev_step) || (array_key_exists($key, $prev_step)) && !is_array($prev_step[$key]) ) { throw new Exception ("\$_SESSION$current_key is not an array"); } eval('$prevStep = $_SESSION' . $current_key . ';'); } else { eval('$_SESSION' . $current_key . ' = ' . $value . ';'); } } } catch (Exception $e) { //TODO:log notice if Session[currentKey] is not an array echo $e->getMessage(); return false; } return eval('array_key_exists(' . $key . ', '.$prev_step.') && !empty($_SESSION' . $current_key . ');'); } function setSessionTestingData() { $agent = 5; $ticket = 6; $_SESSION = array( 'agent_id' => '2', 'ticket_id' => '3', 'agent' => $agent, 'ticket' => $ticket, 'sub_array' => array( 'sub' => 4, 'agent' => $agent, 'ticket' => $ticket, ), ); return $_SESSION; } $_SESSION = setSessionTestingData(); var_dump(setKey('sub_array:sub:unexistant:none', '4')); !empty($_SESSION['sub_array']['sub']['unexistant']); var_dump(!isset($_SESSION['sub_array']['sub']['unexistant']));
based on BQN87
Output for 7.0.0 - 7.2.0
Notice: Array to string conversion in /in/ODnIk on line 34 Parse error: syntax error, unexpected ')', expecting '(' in /in/ODnIk(34) : eval()'d code on line 1
Process exited with code 255.
Output for 5.4.11 - 5.6.28
Notice: Array to string conversion in /in/ODnIk on line 34 Parse error: syntax error, unexpected ')', expecting '(' in /in/ODnIk(34) : eval()'d code on line 1 bool(false) bool(false)
Output for 5.3.21 - 5.3.29
Parse error: syntax error, unexpected ')', expecting '(' in /in/ODnIk(34) : eval()'d code on line 1 bool(false) bool(false)
Process exited with code 255.