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;
1
  • 3
    Did you mean to assign 0<br /> to $tame in the first echo statement? Commented Jan 19, 2013 at 2:19

8 Answers 8

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.

Sign up to request clarification or add additional context in comments.

5 Comments

He didn't really imply that, so we don't really know.
True, but I'm trying to be smart ;)
If he is intending for $tame to be an integer this would be the best answer, but for the complete solution you still need to set it equal to zero so I would do this - echo ($tame = (int)$tame), '<br />';
@AustinBrunkhorst nice touch!
Nice, beat mine by 8 characters.
3
$tame = ($tame == null) ? 0 : $tame;
echo $tame . '<br />';

Comments

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

1 Comment

He wants to also set $tame equal to zero.
1
echo ($tame == null ? ($tame = 0) : $tame), '<br />';

Comments

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 />';

2 Comments

"Which could be written as.." - That would include the linebreak in $tame which I don't think he was going for.
@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.
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 />';

1 Comment

He wants to also set $tame equal to zero.
0
echo (is_null($tame)) ? '0<br />' : $tame . '<br />';

2 Comments

This is not the same as what was written. $tame was mutated in the original.
That assignment seems like a mistake. Otherwise the known value of $tame would likely already have a <br /> at the end, then printing an extra br tag.
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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.