I haven't posted on here before so am not sure whether my posting etiquette is up to scratch but I thought I would try my luck on here.
So, I have this PHP function here which I use to create variables without it throwing errors if there is no value set for it
function getIfSet(&$value, $default = "") {
return isset($value) ? $value : $default;
}
One example where I use this would be:
$errormsg = getIfSet($_SESSION["LoginError"]);
if ($errormsg != "") {
unset($_SESSION["LoginError"]);
}
However I have some pages where I use this multiple times so I attempted creating a function for this instead. This was not working (I am very new to PHP however), but I noticed that the following code worked
$a = "errormsg";
$b = "LoginError";
$$a = getIfSet($_SESSION[$b]);
if ($$a != "") {
unset($_SESSION[$b]);
}
So by this logic, I assumed the following function would work too
function get($a, $b) {
$$a = getIfSet($_SESSION[$b]);
if ($$a != "") {
unset($_SESSION[$b]);
}
}
get("errormsg", "LoginError");
However it does not. It instead says Notice: Undefined variable: errormsg on line 78 but I do not quite understand why it would not work, seeing as it works fine when I use the variables $a and $b just not in a function instead. Thank you in advance and sorry if I don't have very efficient coding, I'm new :D