概述
三元运算符 php You’re probably already familiar with PHP’s 您可能已经熟悉PHP的 But there’s a way to build on this concept and increase your 但是有一种方法可以建立在这个概念上,并在此过程中稍微增加 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,则将选择第二个。 观察: 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. 我最喜欢的三元运算符之一是检查是否设置了值,如果未设置,则将变量设置为默认值。 If a color parameter was passed in to the script from the URL, it’s value is assigned to 如果将颜色参数从URL传递到脚本,则其值将分配给 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,则返回其值。 否则,将返回替代方法。 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. 但伟大的冷静带来了重大的责任! 正确使用三元运算符可以使代码更简洁; 滥用三元运算符会使事情变得一团糟。 切勿牺牲代码的可读性或可维护性,以免增加麻烦。 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 在使用三元运算符之前,您应该考虑当前情况的复杂性。 即使您习惯使用它们,也请避免嵌套或堆叠您的操作,因为这可能会导致代码非常混乱并且结果不直观。 对于复杂的情况,仍然最好使用 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. 将三元表达式拆分成多行也是闻所未闻的。 与编程中的大多数事情一样,自由使用空白以提高代码的可读性有多种变体。 As always, readability should be key. 与往常一样,可读性应该是关键。 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/ 三元运算符 phpif
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:if
语句。 它与许多其他编程语言中的对应语言非常相似,并且代表了编程中最基本的概念之一。 if
语句易于理解且易于掌握。 这可能是您习惯的: <?php
if ($coolFactor >= 10) {
$message = "You are one cool dude!";
}
else {
$message = "Sorry, you aren't that cool!";
}
$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)
<?php
$message = ($coolFactor >= 10) ? "You're one cool dude!" : "Sorry, you aren't that cool!";
<?php
$favoriteColor = isset($_GET["color"]) ? $_GET["color"] : "pink";
$favoriteColor
. If it wasn’t, the default value “pink” is assigned instead.$favoriteColor
。 如果不是,则分配默认值“ pink”。 <?php
$favoriteColor = $_GET["color"] ?: "pink";
不要滥用它! (Don’t Abuse It!)
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
语句。 最重要的是,对下一个家伙好,并尝试保持您的代码干净且易于理解。 <?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.";
摘要 (Summary)
最后
以上就是怕孤单薯片为你收集整理的三元运算符 php_使用PHP三元运算符的全部内容,希望文章能够帮你解决三元运算符 php_使用PHP三元运算符所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复