Home > Article > Backend Development > PHP tips to make using the ternary operator easier
Today I discovered a small usage of the PHP ternary operator. This gave my dry brain a little fun!
PHP ternary operator is a concise main usage when assigning values to parameters. . One major usage: The PHP ternary operator allows you to describe the decision code in one line of code, thereby replacing code similar to the following:
<?php if (isset($value)) { $output = $value; } else { $output = 'No value set.'; }
Use the following code to replace:
<?php $output = isset($value) ? $value : 'No value set.';
The two code examples are very concise usage, and in many situations (not all), this is a very practical usage. There is a lot of debate about whether you should use the ternary operator; let me say, this is a tool, Like other tools, it just needs to be used correctly.
The commonly used syntax is (expression)? value if truthy: value if falsy. This expression can be a variable, test whether the variable is true or false:
<?php$output = $value ? $value : 'No value set.';
The problem is: the above example is very common and annoyingly repetitive: writing $value twice feels like a mistake.
Fortunately, I found out today that PHP 5.3
introduced a more concise syntax using the ternary operator. You can learn it from the manual, but here how do we make the above example more concise:
<?php $output = $value ?: 'No value set.';
Look at this It looks familiar, because it is very similar to other abbreviation operators:
<?php $value = $value . $other_value;
converts to:
<?php $value .= $other_value;
For the sake of simplicity, this means that we can abbreviate this way but it does not mean that we canshould be written like this. However, when we write concise code, this way will look clearer, we should be written like this, (and this feature allows us to Even shorter ternary operators in PHP using ?:
Today I discovered a small usage of the PHP ternary operator. This gave my dry brain a little fun!PHP ternary operator is a concise way to assign values to parameters. Main usage. A main usage: PHP ternary operator allows you to describe the judgment code in one line of code, thereby replacing code similar to the following:<?php if (isset($value)) { $output = $value; } else { $output = 'No value set.'; }Use the following code to replace:
<?php $output = isset($value) ? $value : 'No value set.';The second code example is a very concise usage, and in many cases (not all), it is a very practical usage. There are many debates about whether you should use the ternary operator; let me say, this is one Tools are like other tools, they just need to be used correctly or not.The commonly used syntax is (expression)? value if truthy: value if falsy. This expression can be a variable, test whether the variable is true or false False:
<?php $output = $value ? $value : 'No value set.';The problem is: the above example is very common and annoyingly repetitive: writing $value twice seems like a mistake.Fortunately, I found out today that
PHP 5.3
introduced a more concise syntax using the ternary operator. You can learn it from the manual, but here how do we make the above example more concise:<?php $output = $value ?: 'No value set.';This one looks familiar, and that's because it's very similar to other abbreviation operators:
<?php $value = $value . $other_value;
converts to:
<?php $value .= $other_value;For simplicity, this means we can abbreviate it like this but it doesn't mean we Just
should
be written like this. However, when we write concise code, this way will look clearer, weshould
be written like this,(and this feature allows us Use this operator in many cases[this feature allows us to DRY up the ternary operator in many cases])
The above is the detailed content of PHP tips to make using the ternary operator easier. For more information, please follow other related articles on the PHP Chinese website!