【问题标题】:PHP function getting confused with a second parameter/argumentPHP函数与第二个参数/参数混淆
【发布时间】:2014-06-09 08:55:01
【问题描述】:

在我所有脚本的最后,我运行了一个函数closeConnection,即:

closeConnection("success");

function closeConnection($note) {
    if ($note === "success") { $append = "?success"; }
    if ($note === "blank") { $append = "?blank"; }
    mysql_close();
    header("Location: index.php" . $append . "");
    exit();
}

这运行顺利。

但是,我现在希望我的 closeConnection() 函数接受两个参数,以便我可以选择不同的页面进行重定向。这是第二次的样子:

closeConnection("updated", "view");

function closeConnection($note, $header) {
    $header = $header; // Not sure if needed, doesn't work with or without.
    if ($note === "updated") { $append = "?updated"; }
    if ($note === "blank") { $append = "?blank"; }
    mysql_close();
    header("Location: " . $header . ".php" . $append . "");
    exit();
}

想要的结果:重定向到view.php?updated

实际结果:重定向到.php?blank

【问题讨论】:

  • 你的实际结果是“.php?blank”还是“.php?”
  • 如果没有以任何方式设置?updated,为什么您会期望看到它?如果您通过success,唯一的可能性是?success; ?blank 如果你通过 blank 或者如果你通过其他任何东西(例如view)
  • $header = $header; ???
  • $header = $header; => 不需要这样做 ;)
  • 对不起,从我的代码中复制了错误的if 语句(有很多)。正确的应该现在在那里。 $header = $header 在那里是因为我不确定是否需要“拉入”函数的参数......这些对我来说都是新的。实际结果是.php?blank

标签: php function arguments


【解决方案1】:

根据我的经验,您在 closeConnection("updated", "view"); 之前的某个地方调用了 closeConnection("updated");,但您忘记删除它或其他什么。

确保您没有忘记之前的命令,并且您实际上保存了正确的文件。

【讨论】:

    猜你喜欢
    • 2012-06-04
    • 2016-04-08
    • 2021-11-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-30
    • 2023-04-06
    • 2021-06-23
    相关资源
    最近更新 更多