php三元运算符简写
An essential part of programming is evaluating conditions using if/else and switch/case statements. If / Else statements are easy to code and global to all languages. If / Else statements are great but they can be too long.
编程的重要部分是使用if / else和switch / case语句评估条件。 If / Else语句易于编码,并且对所有语言都是全局的。 如果/ Else语句很棒,但是它们可能太长。
I preach a lot about using shorthand CSS and using MooTools to make JavaScript relatively shorthand, so I look towards PHP to do the same. If/Else statements aren't optimal (or necessary) in all situations. Enter ternary operators.
我讲了很多有关使用速记CSS和使用MooTools来使JavaScript相对速记的方法,因此我希望PHP也能做到这一点。 在所有情况下,if / else语句都不是最佳的(或不是必需的) 。 输入三元运算符。
Ternary operator logic is the process of using "(condition) ? (true return value) : (false return value)" statements to shorten your if/else structures.
三元运算符逻辑是使用“(条件)?(真实返回值):(错误返回值)”语句来缩短if / else结构的过程。
三元逻辑是什么样的? (What Does Ternary Logic Look Like?)
/* most basic usage */
$var = 5;
$var_is_greater_than_two = ($var > 2 ? true : false); // returns true
三元逻辑的优点是什么? (What Are The Advantages of Ternary Logic?)
There are some valuable advantages to using this type of logic:
使用这种类型的逻辑有一些宝贵的优点:
- Makes coding simple if/else logic quicker if / else逻辑使编码更简单
- You can do your if/else logic inline with output instead of breaking your output building for if/else statements 您可以将if / else逻辑与输出内联,而不用破坏if / else语句的输出构造
- Makes code shorter 使代码更短
- Makes maintaining code quicker, easier 使维护代码更快,更容易
- Job security? 就业保障?
使用三元运算符的技巧 (Tips for Using Ternary Operators)
Here are a few tips for when using "?:" logic:
以下是使用“?:”逻辑时的一些提示:
- Don't go more levels deep than what you feel comfortable with maintaining. 不要比您对维护感到满意的要深入。
- If you work in a team setting, make sure the other programmers understand the code. 如果您在团队环境中工作,请确保其他程序员理解该代码。
- PHP.net recommends avoiding stacking ternary operators. "Is [sic] is recommended that you avoid "stacking" ternary expressions. PHP's behaviour when using more than one ternary operator within a single statement is non-obvious." PHP.net建议避免堆叠三元运算符。 “建议您避免“堆叠”三元表达式。在单个语句中使用多个三元运算符时,PHP的行为不明显。”
- If you aren't experienced with using ternary operators, write your code using if/else first, then translate the code into ?'s and :'s. 如果您没有使用三元运算符的经验,请先使用if / else编写代码,然后将代码转换为?和:。
- Use enough parenthesis to keep your code organized, but not so many that you create "code soup." 使用足够的括号可以使代码井井有条,但不要太多,以至于创建“代码汤”。
更多样品使用 (More Sample Usage)
Here are a couple more uses of ternary operators, ranging from simple to advanced:
以下是三元运算符的更多用法,从简单到高级:
/* another basic usage */
$message = 'Hello '.($user->is_logged_in() ? $user->get('first_name') : 'Guest');
/* shorthand usage */
$message = 'Hello '.($user->get('first_name') ?: 'Guest');
/* echo, inline */
echo 'Based on your score, you are a ',($score > 10 ? 'genius' : 'nobody'); //harsh!
/* a bit tougher */
$score = 10;
$age = 20;
echo 'Taking into account your age and score, you are: ',($age > 10 ? ($score < 80 ? 'behind' : 'above average') : ($score < 50 ? 'behind' : 'above average')); // returns 'You are behind'
/* "thankfully-you-don't-need-to-maintain-this" level */
$days = ($month == 2 ? ($year % 4 ? 28 : ($year % 100 ? 29 : ($year %400 ? 28 : 29))) : (($month - 1) % 7 % 2 ? 30 : 31)); //returns days in the given month
To learn more about ternary operators and usage, visit PHP.net Comparison Operators.
要了解更多关于三元运营商和使用情况,请访问PHP.net 比较操作符 。
翻译自: https://davidwalsh.name/php-shorthand-if-else-ternary-operators
php三元运算符简写