$_GET 和 URL 参数并检查发送的值
我得到了两个带有参数和值的链接。当我单击任一链接时,它会将我发送到同一个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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
比较两个值时,使用比较运算符 == 而不是赋值运算符 =。您的代码应该如下所示。
When comparing two values, use the comparison operator == not the assignment operator =. Your code should looks like this.
您在条件中使用
=
而不是==
在条件中,如果您使用
1 == $id
,它将避免犯错误>$id = 1
。如果你写
1 = $id
它会给出错误you are using
=
instead of==
in conditionsIn 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如果你尝试
if ($id == 1)
会怎么样? :)'==' 是一个布尔运算符,但我想你知道这一点。
and what if you try
if ($id == 1)
? :)'==' is a boolean operator, but I think you know that.
使用 $id==1 否则你将 $id 设为 1。
== 检查它是否相等,= 使左边的变量成为右边的值。
Use $id==1 else you make $id 1.
== looks if it is equal, = makes the left variable the right value.
首先,将
$_GET
值转换为 int:在您的条件下,您正在为
$id
变量赋值:有错误,您应该将其与数字进行比较:
所以它应该看起来像:
First of all, cast
$_GET
value to int:In your conditions you are assigning values to the
$id
variable:There is mistake, you should compare it to the numbers:
So it should look like: