这条线是什么意思?

时间:2019-09-22 10:52:57

标签: php html forms get isset

$n = isset($_GET["n"]) ? $_GET['n'] : '';

我发现此“方法”是为了避免在输入类型中插入内容之前发生错误。.它可以工作..但我想对此行进行详细说明。谢谢!

2 个答案:

答案 0 :(得分:0)

这称为ternary operator的{​​{1}}缩写

if...else

有一个条件已在左侧检查,如果条件为true,则将执行(condition) ? true : false 之后的语句,否则将执行?之后的语句。

答案 1 :(得分:0)

它叫做三元运算符

三元运算符是if {} else {}结构的简写。而不是这样写:

if ($condition) {
    $result = 'foo' 
} else {
    $result = 'bar'
}

您可以这样写:

$result = $condition ? 'foo' : 'bar';

如果此$condition的值为true,则左侧操作数将分配给$result。如果条件求值为false,将使用右手。

以您为例

如果设置了$_GET["n"]的值,它将采用$_GET["n"]的值。

如果未设置该值,则它将采用('')值。

$n = isset($_GET["n"]) ? $_GET['n'] : '';
相关问题