1.2.1113. Should Use Coalesce¶
PHP 7 introduced the ??
operator, that replaces longer structures to set default values when a variable is not set.
Sample extracted from PHP docs Isset Ternary.
<?php
// Fetches the request parameter user and results in 'nobody' if it doesn't exist
$username = $_GET['user'] ?? 'nobody';
// equivalent to: $username = isset($_GET['user']) ? $_GET['user'] : 'nobody';
// Calls a hypothetical model-getting function, and uses the provided default if it fails
$model = Model::get($id) ?? $default_model;
// equivalent to: if (($model = Model::get($id)) === NULL) { $model = $default_model; }
?>
See also New in PHP 7: null coalesce operator.
1.2.1113.1. Connex PHP features¶
1.2.1113.1.1. Suggestions¶
Replace the long syntax with the short one
1.2.1113.1.2. Specs¶
Short name |
Php/ShouldUseCoalesce |
Rulesets |
|
Exakat since |
0.8.4 |
PHP Version |
With PHP 7.0 and more recent |
Severity |
Major |
Time To Fix |
Quick (30 mins) |
Precision |
Very high |
Examples |
|
Available in |