<?

function get_value_from_session(string $key, mixed $default = null) : mixed
{
	return isset($_SESSION[$key]) ? $_SESSION[$key] : $default;
}

function get_value_from_array(array $ar, string $key, mixed $default = null) : mixed
{
	return isset($ar[$key]) ? $ar[$key] : $default;
}

function get_value_from_2d_array(array $ar, string $key1, string $key2, mixed $default = null)  : mixed
{
	return isset($ar[$key1][$key2]) ? $ar[$key1][$key2] : $default;
}

function get_value(mixed $var) : mixed
{
    return isset($var) ? $var : null;
}


function get_value_or_default(mixed $var, mixed $default = null) : mixed {
	return isset($var) ? $var : $default;
}

function get_value_property_or_default(mixed $var, mixed $property, mixed $default = null) : mixed {
	
	return $var && property_exists($var, $property) ? $var->$property : $default;
}

function show_pdo_errors_if_any($pdo)
{// Check for errors after the query execution
	$errorInfo = $pdo->errorInfo();
	if ($errorInfo[0] != '00000') {
		// If there's an error (the SQLSTATE isn't '00000', which means no error)
		echo "Error: " . $errorInfo[2]; // The third element contains the error message
	} 
}

function add_or_initialize(mixed $ar, mixed $key, mixed $increment = 1, mixed $initial = 0) : mixed {
	return isset($ar[$key]) ? ($ar[$key] += $increment) : $initial;
}

function add_or_initialize_multi_2(mixed $ar, mixed $key1, mixed $key2, mixed $increment = 1, mixed $initial = 0) : mixed {
	return isset($ar[$key1][$key2]) ? ($ar[$key1][$key2] += $increment) : $initial;
}

function add_or_initialize_multi_3(mixed $ar, mixed $key1, mixed $key2, mixed $key3, mixed $increment = 1, mixed $initial = 0) : mixed {
	return isset($ar[$key1][$key2][$key3]) ? ($ar[$key1][$key2][$key3] += $increment) : $initial;
}

?>