1.2.273. Could Be Ternary¶
This control structure may be replaced by a ternary operator.
Th ternary operator may be shorter and easier to read than the full blown if-then-else structure. Depending on the situation, the null-ternary and the coalesce operator may also be a good alternative.
<?php
// original structure
if (empty($a)) {
$b = 1;
} else {
$b = foo();
}
// ternary version :
$b = empty($a) ? 1 : foo();
?>
See also PHP Shorthand If/Else Using Ternary Operators (?:) https://davidwalsh.name/php-shorthand-if-else-ternary-operators and Shorthand comparisons in PHP https://stitcher.io/blog/shorthand-comparisons-in-php.
1.2.273.1. Connex PHP features¶
1.2.273.1.1. Suggestions¶
Update the syntax to use the ternary operator
1.2.273.1.2. Specs¶
Short name |
Structures/CouldBeTernary |
Rulesets |
|
Exakat since |
2.3.5 |
PHP Version |
All |
Severity |
Minor |
Time To Fix |
Quick (30 mins) |
Precision |
High |
Available in |