简体   繁体   English

PHP if语句与isset不起作用

[英]PHP if statement with isset not working

I presumed this was easy code but this doesnt work as intended: 我认为这是简单的代码,但这不能按预期工作:

$id = $_GET['id'];
if (!isset($id)){
    header("HTTP/1.1 404 not found");
    include("404.php");
}
else {
    include('includes/functions.php'); 
}

So i've taken the get parameters off of the URL yet i dont receive a 404 error? 所以我从URL获取了get参数但我没有收到404错误?

Any ideas? 有任何想法吗?

You must do the test on $_GET['id'] directly: 你必须直接在$_GET['id']上进行测试:

if(!isset($_GET['id']) { ... }

Even if $_GET['id'] isn't set, since you tried to assign it to $id , $id starts to exist, so the test returns true and thus you don't have a 404 error. 即使未设置$_GET['id'] ,因为您尝试将其分配给$id$id开始存在,因此测试返回true,因此您没有404错误。

You are checking if $id is set. 您正在检查是否设置了$ id。 And it is : 它是:

$id = $_GET['id'];

So you must check if the get variable is set: 所以你必须检查是否设置了get变量:

isset($_GET['id'])

you have created $id variable in first line. 你在第一行创建了$ id变量。 You should check $_GET['id'] . 你应该检查$_GET['id'] Also you can check this variable for empty. 您也可以将此变量检查为空。

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

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