1.2.117. Cast To Boolean¶
This expression may be reduced to casting to a boolean. This makes the code more readable, and adapt the type of the value to its usage.
<?php
$variable = $condition == 'met' ? 1 : 0;
// Same as
$variable = (bool) $condition == 'met';
$variable = $condition == 'met' ? 0 : 1;
// Same as (Note the condition inversion)
$variable = (bool) $condition != 'met';
// also, with an indentical condition
$variable = !(bool) $condition == 'met';
// This also works with straight booleans expressions
$variable = $condition == 'met' ? true : false;
// Same as
$variable = $condition == 'met';
?>
1.2.117.1. Connex PHP features¶
1.2.117.1.1. Suggestions¶
Remove the old expression and use
(bool)
operator insteadChange the target values from true/false, or 0/1 to non-binary values, like strings or integers beyond 0 and 1.
Complete the current branches with other commands
1.2.117.1.2. Specs¶
Short name |
Structures/CastToBoolean |
Rulesets |
|
Exakat since |
0.8.4 |
PHP Version |
All |
Severity |
Minor |
Time To Fix |
Instant (5 mins) |
Precision |
Very high |
Examples |
|
Available in |