[英]Difference Between null coalescing operator (??) and nullsafe operator (?->) introduced in PHP 8
[英]Is there a way in PHP to use null coalesing/nullsafe operators to default to not set any value?
我只看到用例是空合並可用於鏈接值,但不能省略任何值。
我是否遺漏了一些重要的東西,或者真的沒有速記方法來寫這個:
$model->name = "Example";
if(isset($input->name)) {
$validName = $this->doSomeValidationEventualyReturnNull($input->name);
if($validName) {
$model->name = $validName;
}
}
我可以提出這一行,與空合並有關。
$model->name = isset($input->name) ? ($this->doSomeValidationEventualyReturnNull($input->name) ?? $model->name) : $model->name;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.