I have this definition of a string:
$string = $string1 .' / '.$string2.' / '.$string3;
Is it possible to write in one line conditions for all strings if they not exists, write "0"?
if (!string1) {$string1="0";}
if (!string2) {$string2="0";}
if (!string3) {$string3="0";}
I tried something like:
$string = !$string1 ? "0" : $string1.' / '.!$string2 ? "0" : $string2.' / '.!$string3 ? "0": $string3;
but this way it is not working.
I also tried it with ?? :
$string = $string1 ?? "0" .' / '.$string2 ?? "0" .' / '.$string3 ?? "0";
Your last attempt was very close but your use of the ternary operator needs parenthesis around each of the tests for it to work.
$string = ($string1 ?? "0") .' / '. ($string2 ?? "0") .' / '. ($string3 ?? "0");