0

Here's my code:

function prepare_machine($variables)
{
        foreach ($variables AS $varname => $vartype)
        {
                if (isset($_REQUEST[$varname]))
                {
                        $value = $_REQUEST[$varname];
                        return do_clean($value, $vartype);
                }
                else
                        exit;
        }
}

It is called like this:

prepare_machine(array('order_by_time' => TYPE_BOOLEAN));

it all works fine, but if you have multiple things in the array, for examples;

prepare_machine(array('order_by_time' => TYPE_BOOLEAN, 'order_by_date' => TYPE_BOOLEAN));

it will only do anything with the first one.

Can anybody see what is wrong with my code?

Thanks

0

2 Answers 2

1

You're doing a return ... when you find a match in your inner loop. That's why it only processes one.

Also, you should be using array_key_exists($varname, $_REQUEST) because isset($_REQUEST[$varname]) will fail if $_REQUEST[$varname] is null.

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

2 Comments

Okay, so what should I do instead of the return?
You need to ask why prepare_machine() returns what do_clean() returns. Whatever that answer is, you will need to re-engineer your code. Either prepare_machine() can't process more than one variable per call, or do_clean() needs to not return a "cleaned" value.
0

return returns whatever you give it, and exits the function. You need to change your function somehow so that it only returns after processing all the variables.

1 Comment

How would I do that, then? Thanks

Your Answer

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