三元运算符 php_使用PHP三元运算符

三元运算符 php

You’re probably already familiar with PHP’s if statement. It’s very similar to its counterparts in many other programming languages and represents one of the most fundamental concepts in programming. The if statement is easy to understand and easy to master. This is probably what you’re used to:

您可能已经熟悉PHP的if语句。 它与许多其他编程语言中的对应语言非常相似,并且代表了编程中最基本的概念之一。 if语句易于理解且易于掌握。 这可能是您习惯的:

<?php
if ($coolFactor >= 10) {
    $message = "You are one cool dude!";
}
else {
    $message = "Sorry, you aren't that cool!";
}

But there’s a way to build on this concept and increase your $coolFactor a bit in the process. Allow me to introduce you to the ternary operator, which serves as a shorthand notation for if statements.

但是有一种方法可以建立在这个概念上,并在此过程中稍微增加$coolFactor 。 请允许我向您介绍三元运算符,它是if语句的简写形式。

介绍三元运算符 (Introducing the Ternary Operator)

Consisting of three parts, the ternary operator uses three expressions separated by a question mark and a colon. The question mark follows the test expression and can be thought of as asking, “Well, is it true?” The colon then separates your two possible values, the first of which will be chosen if the test expression is true, and the second if the test expression is false. Observe:

由三部分组成,三元运算符使用由问号和冒号分隔的三个表达式。 问号遵循测试表达式,可以认为是问:“嗯,是真的吗?” 然后,冒号分隔两个可能的值,如果测试表达式为true,则将选择第一个,如果测试表达式为false,则将选择第二个。 观察:

<?php
$message = ($coolFactor >= 10) ? "You're one cool dude!" : "Sorry, you aren't that cool!";

One of my favorite uses for the ternary operator is to check whether or not a value is set and, if it isn’t, set the variable to a default value.

我最喜欢的三元运算符之一是检查是否设置了值,如果未设置,则将变量设置为默认值。

<?php
$favoriteColor = isset($_GET["color"]) ? $_GET["color"] : "pink";

If a color parameter was passed in to the script from the URL, it’s value is assigned to $favoriteColor. If it wasn’t, the default value “pink” is assigned instead.

如果将颜色参数从URL传递到脚本,则其值将分配给$favoriteColor 。 如果不是,则分配默认值“ pink”。

Since PHP 5.3 it is possible to abbreviate ternary statements even more by excluding the middle expression. If the test expression evaluates true in a boolean context, its value is returned. Otherwise, the alternative is returned instead.

从PHP 5.3开始,通过排除中间表达式,甚至可以进一步简化三元语句。 如果测试表达式在布尔上下文中评估为true,则返回其值。 否则,将返回替代方法。

<?php
$favoriteColor = $_GET["color"] ?: "pink";

But with great coolness comes great responsibility! Using the ternary operator properly can result in cleaner code; abusing the ternary operator can make things a mess. Never sacrifice readability or maintainability of your code to add a bit of flare.

但伟大的冷静带来了重大的责任! 正确使用三元运算符可以使代码更简洁; 滥用三元运算符会使事情变得一团糟。 切勿牺牲代码的可读性或可维护性,以免增加麻烦。

不要滥用它! (Don’t Abuse It!)

Before using the ternary operator, you should consider the complexity of the situation at hand. Avoid nesting or stacking your operations, even if you’re comfortable using them, as this can lead to very confusing code and unintuitive results. It’s still best to use if statements for complex situations. Above all else, be nice to the next guy and try to keep your code clean and easy to understand.

在使用三元运算符之前,您应该考虑当前情况的复杂性。 即使您习惯使用它们,也请避免嵌套或堆叠您的操作,因为这可能会导致代码非常混乱并且结果不直观。 对于复杂的情况,仍然最好使用if语句。 最重要的是,对下一个家伙好,并尝试保持您的代码干净且易于理解。

It is also not unheard of to split ternary expressions into multiple lines. As with most things in programming, there are many variations on using whitespace liberally to improve the readability of your code.

将三元表达式拆分成多行也是闻所未闻的。 与编程中的大多数事情一样,自由使用空白以提高代码的可读性有多种变体。

<?php
$message = $isWinner
    ? "Congratulations! You just won a whole bunch of money and prizes!"
    : "Sorry, you didn't get any money or prizes this time.";

As always, readability should be key.

与往常一样,可读性应该是关键。

摘要 (Summary)

The ternary operator may look a little weird at first but takes very little effort to master and is very easy to explain to others who may be maintaining your code in the future. With a little bit of practice you’ll be able to give your PHP code an extra dose of awesome and clear out a tiny bit of confusion from your life.

三元运算符乍一看可能有点怪异,但掌握起来却花费很少的精力,并且很容易向将来可能维护您代码的其他人解释。 通过一些实践,您将能够为您PHP代码带来更多令人赞叹的效果,并消除您生活中的一点点困惑。

Image via Arman Zender / Shutterstock

图片来自Arman Zender / Shutterstock

翻译自: https://www.sitepoint.com/using-the-ternary-operator/

三元运算符 php

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值