$ array []和array_push不起作用

时间:2013-11-07 16:39:46

标签: php arrays array-push

我正在测试一个简单的兑换系统,但它不起作用。 当用户输入有效代码时,它不会将其添加到“已使用”数组中。

以下是完整代码:

<?php

$submit = $_POST['submit'];
$code   = $_POST['code'];

$used = array(
    "swig",
    "sworgle"
);

$valid = array(
    "swug",
    "sweggle"
);

if($submit){

if(in_array($code, $valid)){
    if(!in_array($code, $used)){
    echo "Congratulations! That is a correct code. <a href='index.php'>Click here to go back</a>";
    $used[] = $code;
} elseif(in_array($code, $used))
    echo "This code has already been used. <a href='index.php'>Click here to go back</a>";
} elseif(!in_array($code, $valid))
    echo "This code is invalid <a href='index.php'>Click here to go back</a>.";

}
else
    echo "Nice try. <a href='index.php'>Go back</a>.";

?>

我尝试过array_push($ used,$ code);但这也不起作用。

1 个答案:

答案 0 :(得分:0)

我认为你可能已经在数组中找到参数的代码,我建议你让变量打印 $ code