7
The problem is to present a result flexing to the plural if necessary, or "no":
Let ni>=0 (number of items):
switch(true){
case $ni==0:
$html = ' (nenhum)';
break;
case $ni==1:
$html = ' (1 item)';
break;
case $ni>1:
$html = ' ('.$ni.' itens)';
break;
}
This is working, now with ifs, a little smaller:
if($ni > 1){
$html = ' ('.$ni.' itens)';
} else if($ni == 1){
$html = ' (1 item)';
} else {
$html = ' (nenhum)';
}
It works too, but now the condensed form I tried to make:
$html = ($ni > 1)? ' ('.$ni.' itens)' : ($ni==1)? ' (1 item)' : ' (nenhum)' ;
This does not work, it responds "none" correctly, "1 item" correctly, but for ni>1 always responds "1 item".
The idea is to condense an if, Else if, Else into a line, but not if it is possible, since the logic seems correct. There is another condensed form?
Just one observation, I think the correct term would be "ternary operator" rather than "condensed form".
– Kayo Bruno