science-ation/helper.inc.php

75 lines
2.1 KiB
PHP
Raw Permalink Normal View History

<?
function get_value_from_session(string $key, mixed $default = null): mixed
{
return isset($_SESSION[$key]) ? $_SESSION[$key] : $default;
}
function get_value_from_array(mixed $ar, string $key, mixed $default = null): mixed
{
if (is_array($ar)) {
return isset($ar[$key]) ? $ar[$key] : $default;
} else {
return $default;
}
}
function get_value_from_2d_array(array $ar, string $key1, string $key2, mixed $default = null): mixed
2024-12-19 22:29:06 -05:00
{
return isset($ar[$key1][$key2]) ? $ar[$key1][$key2] : $default;
}
function get_value_from_3d_array(array $ar, string $key1, string $key2, string $key3, mixed $default = null): mixed
{
return isset($ar[$key1][$key2][$key3]) ? $ar[$key1][$key2][$key3] : $default;
}
function get_value(mixed $var): mixed
{
return isset($var) ? $var : null;
}
function get_value_or_default(mixed $var, mixed $default = null): mixed
{
return $var && isset($var) ? $var : $default;
2024-12-17 01:34:35 -05:00
}
function get_value_property_or_default(mixed $var, mixed $property, mixed $default = null): mixed
{
return $var && isset($var->$property) ? $var->$property : $default;
2024-12-20 15:48:48 -05:00
}
2024-12-19 22:29:06 -05:00
function show_pdo_errors_if_any($pdo)
{ // Check for errors after the query execution
2024-12-17 01:34:35 -05:00
$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
}
2024-12-17 01:34:35 -05:00
}
function check_for_pdo_errors($pdo)
{
$errorInfo = $pdo->errorInfo();
if ($errorInfo[0] != '00000')
return true;
else
return false;
}
function add_or_initialize(mixed $ar, mixed $key, mixed $increment = 1, mixed $initial = 0): mixed
{
2024-12-19 22:29:06 -05:00
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
{
2024-12-19 22:29:06 -05:00
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
{
2024-12-19 22:29:06 -05:00
return isset($ar[$key1][$key2][$key3]) ? ($ar[$key1][$key2][$key3] += $increment) : $initial;
}
?>