php - 从API输出空字符串
所以我用下面的数组来传入 $record params
但当API的某一返回值为null时,我收到了错误。
这里是数组。
$office->load([
'post_title' => $record->office,
'location_id' => $record->location_id,
'location' => $record->location,
'business_unit' => $record->business_unit,
'type_id' => $record->type_id,
'brand_id' => $record->brand_id,
]);
My $record->brand_id
被返回为null,这让我的整个脚本崩溃,有什么方法可以让我把null输出为一个字符串,并把$record->brand_id包在什么地方?
【回答】:
如果你知道哪些值可能是 null
并且对API输出没有影响,你可以尝试检查空值的操作符,比如说 null coalescing operator (??)
.
$default_value = 'default';
$office->load([
'post_title' => $record->office,
'location_id' => $record->location_id,
'location' => $record->location,
'business_unit' => $record->business_unit,
'type_id' => $record->type_id,
'brand_id' => $record->brand_id ?? $default,
]);