理解php中的三元运算符 [英] understading the ternary operator in php
本文介绍了理解php中的三元运算符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在阅读别人的代码,他们有这样一行:
I'm reading someone else's code and they have a line like this:
$_REQUEST[LINKEDIN::_GET_TYPE] = (isset($_REQUEST[LINKEDIN::_GET_TYPE])) ? $_REQUEST[LINKEDIN::_GET_TYPE] : '';
我只想确保我遵循此规则.我可能最终已经弄清楚了它的逻辑.
I just want to make sure I follow this. I might have finally figured out the logic of it.
这是正确的吗?
如果设置了$_REQUEST[LINKEDIN::_GET_TYPE]
,则将其分配给自己. (意味着什么都不做),否则将其设置为空字符串. (这将暗示在脚本的其他部分中,NULL(未定义)和"将不会被视为相同.)
If $_REQUEST[LINKEDIN::_GET_TYPE]
is set, then assign it to itself. (meant as a do-nothing condition) otherwise set it to a null string. (Would imply that NULL (undefined) and "" would not be treated the same in some other part of the script.)
推荐答案
您发布的三元运算符的作用类似于单行if-else
,如下所示
The ternary operator you posted acts like a single line if-else
as follows
if (isset($_REQUEST[LINKEDIN::_GET_TYPE])) {
$_REQUEST[LINKEDIN::_GET_TYPE] = $_REQUEST[LINKEDIN::_GET_TYPE];
} else {
$_REQUEST[LINKEDIN::_GET_TYPE] = '';
}
您可以简化为
if (!(isset($_REQUEST[LINKEDIN::_GET_TYPE]))) {
$_REQUEST[LINKEDIN::_GET_TYPE] = '';
}
这篇关于理解php中的三元运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文