1

How can I make this a one or two line shorthand ternary?

if ($tame == null): 
    echo $tame = '0' . '<br />';
else: 
    echo $tame . '<br />';
endif;
all
  • 21
  • 2
acctman
  • 4,229
  • 30
  • 98
  • 142

8 Answers8

4

Looks like the OP is expecting $tame to be an integer value. Why else would he write zero for null?

If this is the case. This is the shortest solution.

echo (int)$tame.'<br/>';

That will echo 0 for null or a numeric value. There is also no reason to set $tame to zero, because PHP treats null and zero the same.

Reactgular
  • 52,335
  • 19
  • 158
  • 208
3
$tame = ($tame == null) ? 0 : $tame;
echo $tame . '<br />';
mellamokb
  • 56,094
  • 12
  • 110
  • 136
2
echo $tame == null ? $tame = '0<br />' : $tame.'<br />';
sachleen
  • 30,730
  • 8
  • 78
  • 73
1
echo ($tame == null ? ($tame = 0) : $tame), '<br />';
Austin Brunkhorst
  • 20,704
  • 6
  • 47
  • 61
1

The ternary operator is essentially an if..else statement in one line.

if (condition) {
    action(a);
} else {
    action(b);
}

turns into:

action( condition ? a : b );

What you have is:

if ($tame == null) {
    echo $tame = '0' . '<br />';
} else {
    echo $tame . '<br />';
}

which could be written as:

echo $tame == null ? $tame = '0<br />' : $tame . '<br />';

However, this ternary statement is relatively complex, given that $tame is being mutated as part of the statement. Code written this way is harder to read, and therefor difficult to debug, which makes it more prone to errors. It would be better overall if you wrote the code in the more verbose form:

if ($tame == null) {
    $tame = '0';
}
echo $tame . '<br />';
zzzzBov
  • 174,988
  • 54
  • 320
  • 367
  • "Which could be written as.." - That would include the linebreak in `$tame` which I don't think he was going for. – Austin Brunkhorst Jan 19 '13 at 02:31
  • @AustinBrunkhorst, that's what he'd be getting whether or not he intended it. As I said: harder to read = difficult to debug = prone to errors. – zzzzBov Jan 19 '13 at 02:32
0
echo (is_null($tame)) ? '0<br />' : $tame . '<br />';
John Conde
  • 217,595
  • 99
  • 455
  • 496
  • This is not the same as what was written. `$tame` was mutated in the original. – zzzzBov Jan 19 '13 at 02:23
  • That assignment seems like a mistake. Otherwise the known value of `$tame` would likely already have a `
    ` at the end, then printing an extra br tag.
    – joemaller Jan 19 '13 at 02:39
0

How about one more way :)

echo $tame . (!is_null($tame)?:'0'). '<br />';

as you can see I omitted the middle part, since php 5.3 you can do it like this

The ternary part is only the part which is needed to be printed you can still do it like this for older php

echo $tame . (is_null($tame)?'0':''). '<br />';
Hawili
  • 1,649
  • 11
  • 15
0

If $tame is null or an integer, you can skip the logic altogether:

echo sprintf("%d<br />", $tame);

This assumes you didn't mean to assign "0<br />" to $tame in the second line, as pointed out by @mellamokb.

joemaller
  • 19,579
  • 7
  • 67
  • 84