1

I have the following code:

include_once("../content/includes/connect.php");
include_once("_functions.php");

//TODO: support sending variables
$check = true;
$callback = "error";

foreach ($_GET as $key => $value) {
    echo "Key: {$key}<br>";
    echo "Value: {$value}<br>";
    echo "<pre>";
    print_r(checkRules("register", $key, $value));
    echo "</pre>";
    list($pass, $errormessage) = checkRules("register", $key, $value);
    echo "Pass: {$pass}<br>";
    echo "Errormessage: {$errormessage}<br><br>";
    if (!$pass) {
        $check = false;
        $callback = "error";
        break;
    }
}

if ($check) {
    $callback = "register_success";
}

echo json_encode(array(
    "callback" => $callback
));

SQL::close();

And this gives me the following HTML page:

Key: email
Value: [email protected]

Array
(
    [pass] => 1
    [errormessage] => 
)

Pass:
Errormessage:

{"callback":"error"}

Now I do not get why the list($pass, $errormessage) = checkRules("register", $key, $value); does not work, when I clearly see that with print_r() it has the results.

5
  • First, what is the real error message? Second, what do you mean don't work? The error is on the call of the function or inside it? Commented Dec 6, 2013 at 13:54
  • What does var_dump prints? It looks like pass is a boolean. Please also paste the code for checkRules. Commented Dec 6, 2013 at 13:55
  • 3
    @DontVoteMeDown He's not getting an error message. His problem is that the variables are not getting the values returned by the function when he uses list($pass, $errormessage) assignment. Commented Dec 6, 2013 at 13:56
  • 1
    @DontVoteMeDown The question is very clear if you read it. Commented Dec 6, 2013 at 13:56
  • @meagar if he's asking, is not very clear... Commented Dec 9, 2013 at 10:28

3 Answers 3

3

list(...) = array assignment only works when the array is indexed, but checkRules returns an associative array.

You need to write:

$result = checkRules("register", $key, $value);
$pass = $result['pass'];
$errormessage = $result['errormessage'];

or change checkRules to return an indexed array (not my preference -- indexed arrays should only be used for uniform data).

You could also write:

list($pass, $errormessage) = array_values(checkRules("register", $key, $value));

but I think it's generally poor practice to depend on the order of elements in an associative array.

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

Comments

2

You're returning an array with string keys. You need to return an array with numeric indices instead, as list expects.

checkRules should be returning [1, ""], not array("pass" => 1, "errormessage" => "").

Comments

1

Change line like this:

list($pass, $errormessage) = array_values(checkRules("register", $key, $value));

list can't work with associative arrays :)

3 Comments

Definitely won't work. extract pulls the variables out into the local namespace, the result can't be assigned to list. This will just give you a syntax error.
This can work with if you use array_values instead of extact.
Thank you, guys, messed this funcion up in my mind. Updated the answer.

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.