警告:PHP 中出现未定义的数组键错误
Warning: Undefined array key error in PHP
这是我的代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>My First Form</title>
</head>
<body>
<form action="form.php" method="get">
Name: <input type="text" name="username">
<br>
Age: <input type="number" name="age"><br>
<input type="submit">
</form>
<?php
echo $_GET["username"];
echo "</br>";
echo $_GET['age'];
?>
</body>
</html>
我遇到一个错误:
Warning: Undefined array key "username" in C:\Users\nambi\Desktop\PHP Tutorial\form.php on line 18
Warning: Undefined array key "age" in C:\Users\nambi\Desktop\PHP Tutorial\form.php on line 20.
回显前先检查数组key-value是否存在,如下所示:
<?php
if (isset($_GET["username"], $_GET["age"])) {
echo $_GET["username"];
echo "</br>";
echo $_GET['age'];
}
这是我的代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>My First Form</title>
</head>
<body>
<form action="form.php" method="get">
Name: <input type="text" name="username">
<br>
Age: <input type="number" name="age"><br>
<input type="submit">
</form>
<?php
echo $_GET["username"];
echo "</br>";
echo $_GET['age'];
?>
</body>
</html>
我遇到一个错误:
Warning: Undefined array key "username" in C:\Users\nambi\Desktop\PHP Tutorial\form.php on line 18
Warning: Undefined array key "age" in C:\Users\nambi\Desktop\PHP Tutorial\form.php on line 20.
回显前先检查数组key-value是否存在,如下所示:
<?php
if (isset($_GET["username"], $_GET["age"])) {
echo $_GET["username"];
echo "</br>";
echo $_GET['age'];
}