$_GET 和 URL 参数并检查发送的值

发布于 2025-01-03 17:40:18 字数 491 浏览 0 评论 0原文

我得到了两个带有参数和值的链接。当我单击任一链接时,它会将我发送到同一个challenge.php 页面,我想要查找参数的值并根据参数在URL 中的值显示页面。 这个脚本似乎不起作用。

<a href="http://www.site.com/challenge.php?id=1">Complete Challenge 1</a>
<a href="http://www.site.com/challenge.php?id=2">Complete Challenge 2</a>


$id = $_GET['id'];
if ($id=1) {
        echo "This is challenge 1 completed";
} elseif ($id=2) {
    echo "This is challenge 2 completed";
 else {
    echo "The URL parameters didn't work";
}

I got two links with a parameter with a value. When I click either link it will send me to the same challenge.php page, I'm wanting to find the value of the parameter and display the page according to what value the parameter has in the URL.
This script doesn't seem to be working.

<a href="http://www.site.com/challenge.php?id=1">Complete Challenge 1</a>
<a href="http://www.site.com/challenge.php?id=2">Complete Challenge 2</a>


$id = $_GET['id'];
if ($id=1) {
        echo "This is challenge 1 completed";
} elseif ($id=2) {
    echo "This is challenge 2 completed";
 else {
    echo "The URL parameters didn't work";
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

空气里的味道 2025-01-10 17:40:18

比较两个值时,使用比较运算符 == 而不是赋值运算符 =。您的代码应该如下所示。

$id = (int)$_GET['id'];

if ($id == 1) {
        echo "This is challenge 1 completed";
} elseif ($id == 2) {
    echo "This is challenge 2 completed";
 else {
    echo "The URL parameters didn't work";
}

When comparing two values, use the comparison operator == not the assignment operator =. Your code should looks like this.

$id = (int)$_GET['id'];

if ($id == 1) {
        echo "This is challenge 1 completed";
} elseif ($id == 2) {
    echo "This is challenge 2 completed";
 else {
    echo "The URL parameters didn't work";
}
望笑 2025-01-10 17:40:18

您在条件中使用 = 而不是 ==

$id = $_GET['id'];
if (1 == $id) {  // you can use === if you want to compare type also
        echo "This is challenge 1 completed";
} elseif (2 == $id) {
    echo "This is challenge 2 completed";
 else {
    echo "The URL parameters didn't work";
}

条件中,如果您使用 1 == $id ,它将避免犯 错误>$id = 1
如果你写 1 = $id 它会给出错误

you are using = instead of == in conditions

$id = $_GET['id'];
if (1 == $id) {  // you can use === if you want to compare type also
        echo "This is challenge 1 completed";
} elseif (2 == $id) {
    echo "This is challenge 2 completed";
 else {
    echo "The URL parameters didn't work";
}

In condition if you use 1 == $id it will save from making mistakes of $id = 1.
In case if you write 1 = $id it will give error

故事和酒 2025-01-10 17:40:18

如果你尝试 if ($id == 1) 会怎么样? :)
'==' 是一个布尔运算符,但我想你知道这一点。

and what if you try if ($id == 1) ? :)
'==' is a boolean operator, but I think you know that.

囚你心 2025-01-10 17:40:18

使用 $id==1 否则你将 $id 设为 1。

== 检查它是否相等,= 使左边的变量成为右边的值。

Use $id==1 else you make $id 1.

== looks if it is equal, = makes the left variable the right value.

笨笨の傻瓜 2025-01-10 17:40:18

首先,将 $_GET 值转换为 int:

$id = (int) $_GET['id'];

在您的条件下,您正在为 $id 变量赋值:

$id = 1

有错误,您应该将其与数字进行比较:

$id == 1

所以它应该看起来像:

$id = (int) $_GET['id'];
if ($id == 1) {
        echo "This is challenge 1 completed";
} elseif ($id == 2) {
    echo "This is challenge 2 completed";
 else {
    echo "The URL parameters didn't work";
}

First of all, cast $_GET value to int:

$id = (int) $_GET['id'];

In your conditions you are assigning values to the $id variable:

$id = 1

There is mistake, you should compare it to the numbers:

$id == 1

So it should look like:

$id = (int) $_GET['id'];
if ($id == 1) {
        echo "This is challenge 1 completed";
} elseif ($id == 2) {
    echo "This is challenge 2 completed";
 else {
    echo "The URL parameters didn't work";
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文