简体   繁体   English

PHP页面的默认值,URL中没有参数

[英]Default value for PHP page with no argument in URL

I'm new to the PHP game and my problem is one of understanding so apologies for the 101 question. 我是PHP游戏的新手,我的问题是理解101问题的道歉之一。

I have an index.php in a sub-folder on my site. 我的网站上的子文件夹中有一个index.php。 I know that I can pass an argument to the page via the URL. 我知道我可以通过URL将参数传递给页面。

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

If someone navigates to 如果有人导航到

https://example.com/subfolder

without the argument, how do I supply the default 4321? 没有参数,如何提供默认的4321?

The code I've gleaned from other Stack Overflow questions is 我从其他Stack Overflow问题中收集的代码是

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

I've also tried 我也尝试过

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

which I believe is just a different way of writing the same thing. 我认为这只是写同一件事的另一种方式。

The code pulls the argument from the URL when it's supplied and the page works fine, but it doesn't supply the default when the argument isn't supplied. 当提供参数时,代码会将参数从URL中拉出,页面工作正常,但是当未提供参数时,它将不提供默认值。 I suspect it's got something to do with the id variable not being defined if it's not in the URL but I don't know. 我怀疑这与id变量有关(如果不在URL中但我不知道的话)。

Thanks muchly if you can help. 非常感谢您的帮助。

EDIT: Just in case you're right James, I'm including the actual echo line that I'm using (sans domain). 编辑:以防万一你是对的詹姆斯,我包括了我正在使用的实际回声线(无域)。 Everything else is the same as I've typed it above. 其他所有内容都与我上面输入的内容相同。

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>';

EDIT: And when I view the page source, this is what's returned from the PHP 编辑:当我查看页面源代码时,这是从PHP返回的内容

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

This was the code I got to work, I had to add in an extra step. 这是我要工作的代码,我必须添加一个额外的步骤。

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

I'd be interested to know if anyone can work out why the original didn't work though. 我很想知道是否有人可以弄清楚为什么原始版本不起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM