want to use PHP Null coalescing operator (??) with comperison

Zain

if I just want results based on var value set or not? so that I can simply use this

$lab_type=$label['type']??null;

but if want to compare $label['type'] response with using ?? operator, then how to do.

want to change it

 <option value="background" <?= isset($label['type'])?$label['type']=='background'?'selected':'':'selected'?> >Background</option>

don't want to use isset here.

benJ

Change your ternary operator to:

<?= ($label['type'] ?? null) == 'background' ? 'selected' : '' ?>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

PHP - null coalescing operator

PHP Null coalescing operator usage

PHP null coalescing operator confusion - how to use in an if statement?

PHP ternary operator vs null coalescing operator

PHP - when to use error suppressing operator ( @( $obj->var ) ) and when to use null coalescing operator ( ?? )

Unique ways to use the Null Coalescing operator

Is there a way to implement and make use of a "NOT null coalescing" operator?

What is null coalescing assignment ??= operator in PHP 7.4

using PHP's null coalescing operator on an array

PHP null coalescing with ternary operator (again)

PHP - foreach lose reference with null coalescing operator

Why PHP isset and Null coalescing operator is throwing Notice with concatenation operator?

php null coalescing operator in combination with concatenation operator does not work right

Is there a "null coalescing" operator in JavaScript?

Null coalescing operator (??) with return

Null coalescing operator override

Use of coalescing operator in Typescript

How to use null-coalescing operator with nullable date

Can I use `??` (Null coalescing operator) instead of empty?

Is there a fancy and short solution for using the null coalescing operator with constants in PHP?

How am I misusing the null-coalescing operator? Is this evaluating "null" correctly? I want it to show as 'Unknown' if it is null

Null coalescing operator, unpredictable behavior

null-coalescing operator in the getter

Null coalescing operator not mitigating error

Usage of ?? operator (null-coalescing operator)

How to use ternary operator(?:) or Null Coalescing operator(??) to write if-else condition?

sql null in c# and null coalescing operator

Does null coalescing operator call a function twice?

Null coalescing operator in React JS/ Typescript