【问题标题】:Default value for PHP page with no argument in URLURL 中没有参数的 PHP 页面的默认值
【发布时间】:2017-06-27 17:37:23
【问题描述】:

我是 PHP 游戏的新手,我的问题是对 101 问题的理解非常抱歉。

我网站的子文件夹中有一个 index.php。 我知道我可以通过 URL 将参数传递给页面。

https://example.com/subfolder?id=1234

如果有人导航到

https://example.com/subfolder

没有参数,我如何提供默认的 4321?

我从其他 Stack Overflow 问题中收集到的代码是

<?php
  $id = (isset($_GET["id"])) ? $_GET["id"] : "4321";
  echo "<some html>" . $id . "<some more html>";
?>

我也试过

<?php
  if (isset($_GET['id'])) {
    $id = $_GET['id'];
  } else {
    $id = "4321";
  }
  echo "<some html>" . $id . "<some more html>";
?>

我认为这只是写同一件事的不同方式。

当提供参数并且页面工作正常时,代码会从 URL 中提取参数,但在未提供参数时它不提供默认值。我怀疑它与未定义的 id 变量有关,如果它不在 URL 中但我不知道。

如果您能提供帮助,非常感谢。

编辑:以防万一你是对的詹姆斯,我包括我正在使用的实际回声线(无域)。其他的都和我上面打的一样。

echo '<iframe width="70%" padding-bottom="20px" height="900" scrolling="yes" frameborder="no"
        src="https://example.com/eventlist/415c564448271d0f1504/?point_of_sale_id=' . $id . '"></iframe>';

编辑:当我查看页面源代码时,这是从 PHP 返回的内容

<iframe width="70%" padding-bottom="20px" height="900" scrolling="yes" frameborder="no"
        src="https://example.com/eventlist/415c564448271d0f1504/?point_of_sale_id="></iframe>

【问题讨论】:

  • 您的两个代码示例都应该可以正常工作,所以有些东西告诉我,我们还缺少这个难题的另一部分。
  • 如果这些引用 来自您的真实代码,请将它们更改为:" 并重试。只有'" 是PHP 中的有效引号。您可能还想查看:How do I get PHP errors to display?.
  • 谢谢马格努斯。我已经修改了代码。我的代码没有倾斜的。真不知道他们是怎么偷偷溜进去的。

标签: php arguments default


【解决方案1】:

这是我要工作的代码,我必须在一个额外的步骤中添加。

<?php
  $id = $_GET["id"];
  $linkId = (isset($id)) ? $id : "4321";
  echo "<some html>" . $linkId . "<some more html>";
?>

我很想知道是否有人能弄清楚为什么原版不起作用。

【讨论】:

    猜你喜欢
    • 2013-12-17
    • 1970-01-01
    • 2021-07-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多