PHP 页面的默认值 URL 中没有参数
Default value for PHP page with no argument in URL
我是 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
$id = $_GET["id"];
$linkId = (isset($id)) ? $id : "4321";
echo "<some html>" . $linkId . "<some more html>";
?>
我很想知道是否有人能弄清楚为什么原始版本不起作用。
我是 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
$id = $_GET["id"];
$linkId = (isset($id)) ? $id : "4321";
echo "<some html>" . $linkId . "<some more html>";
?>
我很想知道是否有人能弄清楚为什么原始版本不起作用。