PHP Error : Unparenthesized `a ? b : c ? d : e` is deprecated. Use either `(a ? b : c) ? d : e` or `a ? b : (c ? d : e)`
This change in php has been done to remove ambiguity in the decision tree so that there is an explicit order of condition execution.
The deprecation warning is reproduced here:
Code:
$allLanguages = ['en', 'es', 'fr'];
$values = ['es' => 'Spanish1'];
$filesContent = [
'foo' => [
'es' => ['bar' => 'Spanish2'],
'fr' => ['bar' => 'French']
]
];
$fileName = 'foo';
$key = 'bar';
$original = [];
foreach ($allLanguages as $languageKey) {
$original[$languageKey] =
isset($values[$languageKey])
? $values[$languageKey]
: isset($filesContent[$fileName][$languageKey][$key])
? $filesContent[$fileName][$languageKey][$key]
: '';
}
var_export($original);
Output:
Deprecated: Unparenthesized `a ? b : c ? d : e` is deprecated. Use either `(a ? b : c) ? d : e` or `a ? b : (c ? d : e)` in /in/TG4g2 on line 17
array (
'en' => '',
'es' => 'Spanish2',
'fr' => 'French',
)
As a human-reader of your script, I would assume the reading of your condition as left to right -- but this would place Spanish1
as the output value.
Even before php7.4, the output isSpanish2
because the latter fork in the decision tree is given precedence.
To avoid this, you must wrap your conditions in parentheses to dictate exactly how the order of execution should be handled.
Also, I agree with @Laurel that in php7 it is time for you to embrace the syntactic sugary sweetness that is the null coalescing operator. This will avoid precedence issues and the need to use parentheses, but depending on your desired results, you may need to reorder your conditions.
Priority to $values
: (Demo)
$original[$languageKey] =
$values[$languageKey]
?? $filesContent[$fileName][$languageKey][$key]
?? '';
Priority to $filesContent
: (Demo)
$original[$languageKey] =
$filesContent[$fileName][$languageKey][$key]
?? $values[$languageKey]
?? '';
P.s. IIRC, the php manual advises against the use of nested ternaries/conditionals like this on the basis of code clarity. I don't mind this scenario and I like the avoidance of code bloat, but other devs may take a more purist stance.
PHP error: Unparenthesized `a ? b : c ? d : e` is deprecated
It looks like you put the parentheses in the wrong place. Try this:
<li class="{{ (\Request::is('stocks/*') ? ' open' : ( Request::is('stocks') ? ' open' : ( Request::is('defective/*') ? ' open' : '' ) ) ) }}">
You also may be able to simplify it:
<li class="{{ ( ( \Request::is('stocks/*') || Request::is('stocks') || Request::is('defective/*') ) ? ' open' : '' ) }}">
Unparenthesized `a ? b : c ? d : e` is deprecated. Use either `(a ? b : c) ? d : e` or `a ? b : (c ? d : e)`
use old('parent_id') ?? $category['parent_id'] ?? ''
{!! General::selectMultiLevel('parent_id', $categories, ['class' => 'form-control', 'selected' => old('parent_id') ?? $category['parent_id'] ?? '', 'placeholder'=>'---Chose Category---' ]) !!}
Wordpress PHP Fatal error: Unparenthesized `a ? b : c ? d : e` is not supported. Use either `(a ? b : c)
You can try:
$taxonomy = get_the_terms($post->ID, (is_singular('post') ? 'category' : (is_singular('blog') ? 'category' : (is_singular('event') ? 'category' : 'vacancy_category'))))
However even though this doesn't ocupy too much space, I feel like it's hard to read. You could use an if else instead to make it more readable.
Updating From PHP 7.3 to PHP 7.4 Throws Fatal Error and Won't Load WordPress Site
Nested ternary expressions are deprecated since PHP 7.4 so you should try chancing the failing code line to:
$post_type = $image ? "" : (get_post_format($post->ID) != "" ? get_post_format($post->ID) : "standard");
Related Topics
PHP Function with Unlimited Number of Parameters
PHP JSON Encode - Malformed Utf-8 Characters, Possibly Incorrectly Encoded
How to Put PHP Inside JavaScript
Allow Re-Sending New Order Notification in Woocommerce 5+
Preg_Replace: Add Number After Backreference
Why Are "Echo" Short Tags Permanently Enabled as of PHP 5.4
Sort an Array by a Child Array's Value in PHP
How to Check If a Row Exist in the Database Using Pdo
PHP Flush/Ob_Flush Not Working
Get Element by Classname with Domdocument() Method
How to Get a Div Content in PHP
Soap PHP Fault Parsing Wsdl: Failed to Load External Entity
Convert Jpg Image to Gif, Png & Bmp Format Using PHP
How to Compile on Linux to Share with All Distributions