首页  >  文章  >  后端开发  >  如何使用与号 (&) 将多个变量传递到 URL 中的另一个页面?

如何使用与号 (&) 将多个变量传递到 URL 中的另一个页面?

Linda Hamilton
Linda Hamilton原创
2024-10-26 12:37:29992浏览

How can I pass multiple variables to another page in a URL using the ampersand (&)?

在 URL 中将多个变量传递到另一个页面

为了通过 URL 参数实现跨页面共享多个变量,仅使用会话可能看起来有限制。但是,通过合并与号 (&) 字符,可以克服此限制。

解决方案:

让我们重新审视问题中的代码:

第 1 页:

<code class="php">session_start();
$event_id = $_SESSION['event_id'];
echo $event_id;

$url = "http://localhost/main.php?email=" . $email_address . "&amp;event_id=" . $event_id;
//                                              ^ add ampersand here</code>

第 2 页:

<code class="php">if (isset($_GET['event_id'])) {
    $event_id = $_GET['event_id'];
}
echo $event_id;</code>

通过在串联变量之间添加 & 符号,我们可以有效地将它们粘合在一起一起在 URL 中。这样可以确保两个变量都可以在第 2 页上检索到:

<code class="php">$event_id = $_GET['event_id']; // successfully retrieved</code>

通过这种方式,您可以有效地在 URL 中传递多个变量,从而实现页面之间的无缝数据交换。

以上是如何使用与号 (&) 将多个变量传递到 URL 中的另一个页面?的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn