当php开发时页面提示Notice: Undefined index: name in D:\Program Files\JetBrains\phpSpace\hello\hello.php on line 2错误是因为用到了$_GET['name']这样的方法接收form表单数据,我的php错误如图。

出现错误的代码,虽然不影响功能,但是很不美观。
<?php
if( $_GET['name'] ||$_GET['pwd']) { //两处错误出现位置
echo "欢迎:". $_GET['name']. "<br />";
echo "这是你的密码: ". $_GET['pwd'];
exit();
}
?>
<html>
<body>
<form action = "<?php $_PHP_SELF ?>" method = "GET">
用户名: <input type ="text" name = "name" />
密码: <input type = "text" name = "pwd" />
<input type = "submit" value="提交"/>
</form>
</body>
</html>于是我添加了一个判空的函数就不会有上面的两处的错误了,把代码修改成如下这种形式。
<?php
if( _get("name") || _get("pwd")) { //把上面的代码改成这种形式
echo "欢迎:". $_GET['name']. "<br />";
echo "这是你的密码: ". $_GET['pwd'];
exit();
}
//添加一个判空函数
function _get($str){
$val = !empty($_GET[$str]) ? $_GET[$str] : null;
return $val;
}
?>
<html>
<body>
<form action = "<?php $_PHP_SELF ?>" method = "GET">
用户名: <input type ="text" name = "name" />
密码: <input type = "text" name = "pwd" />
<input type = "submit" value="提交"/>
</form>
</body>
</html>Notice: Undefined index:错误就轻松被解决了。如图所示,干干净净了。
