三元运算符使 html 结束标记不可见
Ternary Operator makes html closing tags not visible
我正在使用三元运算符为空字段创建一个非常基本和简单的验证。
代码:
<?php
echo "<span class='error'>" . $error = (isset($_POST['naam']) && empty($_POST['naam'])) ? 'Required field' : test_input(isset($_POST['naam'])) . "</span>"
?>
整行
echo "<div class='form-group'><Label for='name'>Voorstelling naam</Label><input type='text' name='naam' value='".$row['naam']."' placeholder='Naam'><span class='error'>" . $error = (isset($_POST['naam']) AND empty($_POST['naam'])) ? 'Dit is een verplicht veld' : test_input(isset($_POST['naam'])) . "</span></div>";
代码工作正常,但浏览器中的 </span>
标签不是 working/visible
我觉得你的代码不完整。
您可以在 HTML 内添加 PHP 代码:
<!-- PHP INSIDE HTML -->
<span class="error"><?php echo (empty($_POST['naam']))? "Required field" : test_input($_POST['naam']); ?></span>
或在 PHP 中生成 HTML :
<?php
// HTML INSIDE PHP
$html = '<span class="error">'
. (empty($_POST['naam'])? "Required field" : test_input($_POST['naam']))
. '</span>';
echo $html;
?>
补充说明:
1 - 我认为你不需要测试 isset
和 empty
因为 empty
return true
如果变量不存在并且如果字段未正确传输,我相信你也想显示错误?
2 - 你不需要在调用 test_input()
函数时使用 isset()
因为如果你的脚本到达这一步,你已经知道 $_POST['naam']
存在并且是不为空,通过这样做,您将使用 isset 的 return 作为参数而不是字段的实际值。
使用更新后的代码,尝试删除 $error =
赋值部分。您还将 isset
return 值传递给 test_input
,这可能不是您想要的。
由于 $error =
分配中的三元组,"</span></div>"
仅附加到检查的失败案例。
echo "<div class='form-group'><Label for='name'>Voorstelling naam</Label><input type='text' name='naam' value='".$row['naam']."' placeholder='Naam'><span class='error'>" .
$error = (isset($_POST['naam']) AND empty($_POST['naam'])) ?
'Dit is een verplicht veld' : // No added "</span></div>" here, doesn't close!
test_input(isset($_POST['naam'])) . "</span></div>"
;
我正在使用三元运算符为空字段创建一个非常基本和简单的验证。
代码:
<?php
echo "<span class='error'>" . $error = (isset($_POST['naam']) && empty($_POST['naam'])) ? 'Required field' : test_input(isset($_POST['naam'])) . "</span>"
?>
整行
echo "<div class='form-group'><Label for='name'>Voorstelling naam</Label><input type='text' name='naam' value='".$row['naam']."' placeholder='Naam'><span class='error'>" . $error = (isset($_POST['naam']) AND empty($_POST['naam'])) ? 'Dit is een verplicht veld' : test_input(isset($_POST['naam'])) . "</span></div>";
代码工作正常,但浏览器中的 </span>
标签不是 working/visible
我觉得你的代码不完整。
您可以在 HTML 内添加 PHP 代码:
<!-- PHP INSIDE HTML -->
<span class="error"><?php echo (empty($_POST['naam']))? "Required field" : test_input($_POST['naam']); ?></span>
或在 PHP 中生成 HTML :
<?php
// HTML INSIDE PHP
$html = '<span class="error">'
. (empty($_POST['naam'])? "Required field" : test_input($_POST['naam']))
. '</span>';
echo $html;
?>
补充说明:
1 - 我认为你不需要测试 isset
和 empty
因为 empty
return true
如果变量不存在并且如果字段未正确传输,我相信你也想显示错误?
2 - 你不需要在调用 test_input()
函数时使用 isset()
因为如果你的脚本到达这一步,你已经知道 $_POST['naam']
存在并且是不为空,通过这样做,您将使用 isset 的 return 作为参数而不是字段的实际值。
使用更新后的代码,尝试删除 $error =
赋值部分。您还将 isset
return 值传递给 test_input
,这可能不是您想要的。
由于 $error =
分配中的三元组,"</span></div>"
仅附加到检查的失败案例。
echo "<div class='form-group'><Label for='name'>Voorstelling naam</Label><input type='text' name='naam' value='".$row['naam']."' placeholder='Naam'><span class='error'>" .
$error = (isset($_POST['naam']) AND empty($_POST['naam'])) ?
'Dit is een verplicht veld' : // No added "</span></div>" here, doesn't close!
test_input(isset($_POST['naam'])) . "</span></div>"
;