PHP Null Coalescing Operators: Do You Use Them?

preview_player
Показать описание
A quick video about the PHP syntax which I don't see used enough in the wild.

- - - - -
Support the channel by checking out my products:

- - - - -
Other places to follow:
Рекомендации по теме
Комментарии
Автор

I've been using the null collalesce operator lately because it's so much easier to write.

I really like that assignment one! Wasn't aware of that one. I'll have to give it a try. As soon as you described how it worked, defaults for config popped right into my mind.

Could also be used for default values to insert into a database for a legacy database, or if you simply do not prefer to let the database layer define the default value

JouvaMoufette
Автор

Nice one! I completely forgot about this one. I'm also a fan of using ?: operator

cardboarddignity
Автор

I have been a fan of NULL COALESCING operator and using it as I heard of it as alternative of isset. 😝

nabeelyousafpasha
Автор

I found this has come in quite handy actually. :) Thanks for sharing!!

Jason-wvit
Автор

Awesome thanks, please more of that shorthand syntax

AMoktar
Автор

Very nice! i realy like your videos, can you talk more about queues e.g RabbitMQ?

TheMarcelitto
Автор

Hi Povilas, thank you for this quick review of new(ish) operators. Did you mean to say "PHP 7.4 and PHP 8" (about 10 seconds into the video)?

brucepeterkin
Автор

I use them constantly since they were released

HadToChangeMyName_YoutubeSucks
Автор

Hey I saw in some videos that you read some GIT repositories and qualify if it would be senior code etc etc. do you still do it? I would like sent to you on of mine. I've been working with PHP for 14 years and Laravel 4 years

williamrox
Автор

Is this works with array undefined elements?

Deleterus
Автор

Be carful of using it, It works well with null values not with falsy values.

For example: $key = '' ?? 'default'; is going to be an empty string.

MohamedAbdallah-ltwc
Автор

I have a question I would like to ask you to answer.
In my autoload_classmap.php file, it is generated by composer,
with this line:
=> $baseDir .
- I want to ask why it's not like this:
=> $baseDir . '/app/Http/Controllers/FrontController.php',

- I tried running composer dump-autoload but it still hasn't changed
Please explain and correct instructions from the teacher
thanks alot!

hoanxuanmai
Автор

I love them for cached getters;
protected ?array $someValue = null;
public function getSomeValue() {
return $this->someValue ??= $this->expensiveOperation();
}

mallesbixie