I have read that PHP isset and null coalescing operator used to ignore PHP Notice: Undefined index:
I have seen this post also PHP ternary operator vs null coalescing operator
But I am getting PHP notice with both of them while using them with string concatenation operator:
<?php
$array = ['a'=>'d'];
$c = $array['c'] ?? '';
$d = isset($array['c']) ? $array['c'] : '';
$val = "sgadjgjsd".$array['c'] ?? ''; // PHP Notice: Undefined index: c in /home/cg/root/986045/main.php on line 6
$val2 = "sgadjgjsd".isset($array['c']) ? $array['c'] : ''; // PHP Notice: Undefined index: c in /home/cg/root/986045/main.php on line 7
?>
EDIT:
I know This can be solved by the following methods
1) assigning to variable like
$val = "sgadjgjsd".$c = $array['c'] ?? '';
2) using @
$val = "sgadjgjsd".@$array['c'] ?? '';
3) adding brackets (and as Karsten suggested )
$val = "sgadjgjsd".($array['c'] ?? '');
But I am looking for the reason behind it.