I am making a form, some of which is optional. To show the output of this form, I want to be able to check whether a POST variable has contents. If it does, the script should make a new normal PHP variable with the same value and name as the POST variable, and if not it should make a new normal PHP variable with the same name but the value "Not defined". This is what I have so far:
function setdefined($var)
{
if (!$_POST[$var] == "")
{
$$var = $_POST[$var]; // This seems to be the point at which the script fails
}
else
{
$$var = "Not defined";
}
}
setdefined("email");
echo("Email: " . $email); // Provides an example output - in real life the output goes into an email.
This script doesn't throw any errors, rather just returns "Email: ", with no value specified. I think this is a problem with the way I am using variable variables within a function; the below code works as intended but is less practical:
function setdefined(&$var)
{
if (!$_POST[$var] == "")
{
$var = $_POST[$var];
}
else
{
$var = "Not defined";
}
}
$email = "email"; // As the var parameter is passed by reference, the $email variable must be passed as the function argument
setdefined($email);