1

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);
8
  • 3
    php.net/manual/en/language.variables.scope.php Commented Jan 4, 2013 at 18:34
  • php.net/manual/en/book.array.php Commented Jan 4, 2013 at 18:34
  • php.net/manual/en/function.return.php Commented Jan 4, 2013 at 18:35
  • Read those three ^ and I'm pretty sure we you will find your answer. Commented Jan 4, 2013 at 18:35
  • Read links suggested by @PeeHaa - especially the first one, which deals with scope. Basically, your problem comes from the fact that the new variable is only available inside the setdefined function. Commented Jan 4, 2013 at 18:37

2 Answers 2

2

Why don't you do it this way:

function setdefined($var)
{
    if (isset($_POST[$var]) && !empty($_POST[$var]))
    {
        return $_POST[$var];
    }
    else
    {
        return "Not defined";
    }
}
$email = setdefined('email');
echo("Email: " . $email);

The variable you create in first example is only available inside the function

Sign up to request clarification or add additional context in comments.

3 Comments

I wouldn't use empty tho, because it will return TRUE if $_POST[$var] == 0
That's a better way of doing it, I didn't know about the empty function but thanks for the introduction!
Hmmm. At least add some check to verify the index exists AND it has a value to make sure you don't get warnings on e.g. checkboxes. Good and valid point though. In any case feel free to edit my edit :)
0

You could make that unknown variable submitted by possibly malicious user, global one. To do so, add this to the start of your function:

global $$var ;

This is not only ugly, but maybe unsafe too.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.