当我试图回显玩家的名字时,遇到了一个致命错误。
我对PHP还很陌生,这可能是一个新手的错误,但我已经尝试过了。
这是我的代码:
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>A1S | e-tracker</title>
<link rel="stylesheet" href="css/menu.css">
<link rel="author" href="humans.txt">
</head>
<body>
<form action="#" method="get">
Name: <input type="text" placeholder="Player Full Name" name="pFullname"><br>
Date of Birth: <input type="date" name="pDob"><br>
Weight: <input type="number" name="pWeight" placeholder="Pounds" min="0"><br>
Height: <input type="number" name="pHeight" placeholder="Centimeters" min="0"><br>
<input type="submit">
<?php
$playerName = $_GET['pFullName'];
echo $playerName;
?>
</form>
<script src="script.js"></script>
</body>
</html>
任何帮助都将不胜感激,谢谢!
使用以下代码:
$playerName = isset($_GET['pFullname']) ?$_GET['pFullname'] :"";
echo $playerName;
另一个解决方案(如果您使用的是PHP7):
<?php
$playerName = $_GET['pFullname']??'';
echo $playerName;
??
-空合并运算符
对于需要将三元运算符与isset()结合使用的常见情况,添加了null合并运算符(??)作为语法糖。如果它存在并且不为NULL,则返回其第一个操作数;否则返回第二个操作数。
所以它相当于
$playerName = isset($_GET['pFullname']) ? $_GET['pFullname'] : '';
注意:您使用的是pFullName
而不是pFullname
。后者是控制名称