我有一个表单,在我的字段中输入的值取决于是否有GET请求。
我现在的代码可以正常工作,但是我想知道是否有更高效的编码方式,因为我的真实表单将有约10个输入字段,我不想每次添加新的输入字段时都添加6行代码。
<?php
if (isset($_GET["name"])) {
$name = $_GET["name"];
}
else {
$name = 0;
}
if (isset($_GET["type"])) {
$type = $_GET["type"];
}
else {
$type = 0;
}
if (isset($_GET["other"])) {
$other = $_GET["other"];
}
else {
$other = 0;
}
?>
<input type="text" name="name" value="<?php echo $name; ?>">
<input type="text" name="type" value="<?php echo $type; ?>">
<input type="text" name="other" value="<?php echo $other; ?>">
我尝试着也做了一下。
<?php
$name = $_GET["name"] ?: 0;
?>
这将在我的表单中输入一个值0,但我会收到错误提示注意:未定义索引:名称