3v4l.org

run code in 300+ PHP versions simultaneously
<?php function getKey($session_key, $is_object_of_type = null) { $current_key = ''; $counter = 0; try { $session_keys_array = explode(':', $session_key); if (!$_SESSION) { throw new Exception("\$_SESSION does not Exist"); } $prev_step = $_SESSION; //checking if keys and value exist foreach ($session_keys_array as $key) { $counter++; $current_key .= '[\'' . $key . '\']'; echo 0; if (array_key_exists($key, $prev_step) && count($session_keys_array) > $counter) { echo 1; $prevStep = $prev_step[$key]; var_dump($prevStep); if (!is_array($prev_step) && count($session_keys_array) > $counter) { echo 2; throw new Exception ("\$_SESSION$current_key is not an array"); } } else if ( is_array($prev_step) && array_key_exists($key, $prev_step) && count($session_keys_array) == $counter ) { echo 3; $result = $prev_step[$key]; } else { echo 4; throw new Exception("\$_SESSION$current_key does not Exist"); } } echo 5; //checking if value is of right type if (!empty($is_object_of_type) && !$result instanceof $is_object_of_type) { echo 6; throw new Exception("\$_SESSION$current_key is not instance of $is_object_of_type"); } echo 7; //if we are here that means we have all safe data } catch(Exception $e) { //TODO:change Exception to SessionException and log this echo 8; $result = null; } echo 9; return $result; } 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(); echo getKey('sub_array:sub');
Output for 5.5.24 - 5.5.35, 5.6.8 - 5.6.28, 7.0.0 - 7.0.20, 7.1.0 - 7.1.25, 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.18, 8.3.0 - 8.3.4, 8.3.6
01array(3) { ["sub"]=> int(4) ["agent"]=> int(5) ["ticket"]=> int(6) } 0489
Output for 8.3.5
Warning: PHP Startup: Unable to load dynamic library 'sodium.so' (tried: /usr/lib/php/8.3.5/modules/sodium.so (libsodium.so.23: cannot open shared object file: No such file or directory), /usr/lib/php/8.3.5/modules/sodium.so.so (/usr/lib/php/8.3.5/modules/sodium.so.so: cannot open shared object file: No such file or directory)) in Unknown on line 0 01array(3) { ["sub"]=> int(4) ["agent"]=> int(5) ["ticket"]=> int(6) } 0489

preferences:
159.6 ms | 402 KiB | 197 Q