使用三元条件的两个 if 语句
标题看起来很混乱,但这是我第一次使用三元条件。我读过三元是用来制作内联 if/else 语句的。不使用其他方法是不可能的。这是真的吗?
我想用三元条件来改变它,以进行练习
if (isset($_SESSION['group']
{
if ($_SESSION['item'] == 'A')
{
echo "Right!";
}
}
它只有两个 if 语句。第二个 if 与另一个 if 嵌套。我还读到,要使三元不再可能,只需将其设置为 null 或空字符串。
The title seems confusing but this is my first time using ternary conditions. I've read that ternary is meant to be used to make an inline if/else statement. Using no else is not possible. Is it true?
I want to change this with ternary condition for practice
if (isset($_SESSION['group']
{
if ($_SESSION['item'] == 'A')
{
echo "Right!";
}
}
It has two if statements only. The second if is nested with the other. I've also read that to make a no else possible for ternary, it just have to be set to null or empty string.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这是一个糟糕的例子,因为您可以在嵌套的 if 上使用 AND 运算符:
当然您也可以嵌套三元运算符:
with echo
It's a bad example because you can use an AND-operator on the nested if:
Of course you can nest ternary operator, too:
with echo
更好的是(可读、可维护),使用:
Better still (readable, maintainable), use:
试试这个,我想它可能会起作用=]。
要进一步阅读 Java 中的三元条件/无论您使用什么,请查看 http://www .devdaily.com/java/edu/pj/pj010018
Try this, I think it might work =].
For further reading on ternary conditions in Java/ whatever you're using look at http://www.devdaily.com/java/edu/pj/pj010018
您可以嵌套两个三元语句,如下例所示:
You can nest two ternary statements as this example:
您知道您也可以做到这一点吗?
(isset($_SESSION['group']) && ($_SESSION['item']=='A')) &&($result$c= 1);
或 echo (isset($_SESSION['group']) && ($_SESSION['item']=='A')) ? '你好!':'世界!';Did you know you can do this as well?
(isset($_SESSION['group']) && ($_SESSION['item']=='A')) &&($result$c= 1);
orecho (isset($_SESSION['group']) && ($_SESSION['item']=='A')) ? 'Hello!':'World!';