我正在为工作门户创建小型搜索引擎。我使用以下代码进行搜索。
<form method="get" action="search.php">
<input type="text" name="keyword">
<input type="text" name="location">
</form>
当我搜索时,它通过 url 发送输入并显示结果。
例如:url:localhost/jobportal/search.php?keyword=php&location=India
但是,如果我通过在浏览器中输入url直接打开页面:"本地主机/工作门户/搜索.php",则会显示以下错误。
Notice: Undefined index: keyword in C:'xampp'htdocs'jobportal'search'index.php on line 384
Notice: Undefined index: location in C:'xampp'htdocs'jobportal'search'index.php on line 385
我知道为什么会发生此错误,我只需要知道如何避免此错误。
只需添加检查它们是否存在。使用empty
.-
if(!empty($_GET['keyword'])) {
// use value
}
if(!empty($_GET['location'])) {
// use value
}
empty()
将检查它是否set
并且value
不是null
或false
。