有比以下更有效的东西:
if(isset($x)){
$x += 10;
}else{
$x = 10;
}
Run Code Online (Sandbox Code Playgroud)
考虑到我的代码更复杂,例如:
if(isset($complicated_array[$month][$category][$type])){
$complicated_array[$month][$category][$type] += $value[$month];
}else{
$complicated_array[$month][$category][$type] = $value[$month];
}
Run Code Online (Sandbox Code Playgroud)
我正在寻找类似的东西
$x = add_smart($x, 10);
Run Code Online (Sandbox Code Playgroud)
如果您使用的是 PHP 7,则可以使用Null Coalescing Operator
$x = ($x ?? 0) + 10;
Run Code Online (Sandbox Code Playgroud)