DEV Community

Discussion on: Simple PHP Control Structure Refactoring

Collapse
 
tarialfaro profile image
Tari R. Alfaro • Edited

I think it would have a great use case if you're building a lot of options in a function or class method.

so instead of this:

$key = 'default value';

if (\array_key_exists('key', $options)) {
    $key = $options['key'];
}

# But a bunch more ...

But instead we could do a bunch of these:

$key = $options['key'] ?? 'default value';

Also, I'd like to say sorry. I don't think we should always be doing the regular if then else statement. As you said, it's case-by-case. Developers should just learn the language instead.

I think everyone should understand how the ternary operators work. Their symbols are similar to that of && or ||.

Must've missed it, because I didn't see any examples of the 'Evlis' operators or null coalescing.

Anyways, thank you for the great article. w^