0
<?php echo (isset($var)) ?: $var; ?>

Is this syntax correct? What will this display if $var won't be set, empty string or null? Is it ok to use this?

user2124857
  • 39
  • 1
  • 1
  • 9

7 Answers7

2

This:

<?php echo (isset($var)) ?: $var; ?>

do the same as this:

<?php
 if (isset($var)) {
  // do nothing
 } else {
  echo $var;
 }
?>

So you are trying to display variable if its empty/null/etc...

If function:

<?php $k= (cond ? do_if_true : do_if_false); ?>

$k could be new variable, echo, etc.
cond - isset, $z==$y, etc.

miszczu
  • 1,179
  • 4
  • 19
  • 39
  • 3
    That's actually not true. It's functionally equivalent to: `echo isset($var) ? isset($var) : $var` which is technically not the same as your example. – Colin M Mar 04 '13 at 15:52
  • ($var) ?: '' would solve all of my problems, though as far as I know this isn't equivalent to isset($var) ? $var : '', am I right about this one? – user2124857 Mar 04 '13 at 15:58
  • I just tried to run your example and server didn't display any results without `''`, i get error `syntax error, unexpected ':'`, so it might be necessary – miszczu Mar 04 '13 at 16:04
1

The syntax is correct, the usage is not. Say here:

$var = something();
echo $var ?: 'false';

This is equivalent to:

$var = something();
if ($var) {
    echo $var;
} else {
    echo 'false';
}

or shorthand for $var ? $var : 'false'.

Your example is pointless since it outputs the result of isset($var) (true) if $var is set and $var otherwise.

You need echo isset($var) ? $var : null or if (isset($var)) echo $var, and there's no shortcut for it.

deceze
  • 510,633
  • 85
  • 743
  • 889
0

The code no have sense, generates a notice or echo 1, you can't print a $var which isn't set

Sam
  • 2,950
  • 1
  • 18
  • 26
0

The syntax is fine.

If $var is set then it will output, if it is not, it will throw an Notice about the echo of $var which is unset.

if your error_reporting is set to E_NONE then you will just see a white screen, if the $var is set then you will see the value of $var

amphetkid
  • 31
  • 1
  • 3
0

This will echo $var either way.

Since PHP 5.3, it is possible to leave out the middle part of the ternary operator. Expression expr1 ?: expr3 returns expr1 if expr1 evaluates to TRUE, and expr3 otherwise.

So if $var is set, it echos $var (since that evaluates to TRUE), if its not set to anything or evaluates to false, your manually asking it to echo $var anyway.

ideally you want:

(condition  ?  if_true_then_do_this : if_false_then_do_this)

in shorthand this becomes

   (condition ? ? false)

and since you have specified $var in both places, you will get $var, either way. You want this:

echo ($var?:"null");
Husman
  • 6,819
  • 9
  • 29
  • 47
0

Nope, this syntax is incorrect.
By the time of echoing, all variables have to be set and properly formatted.
Otherwise means a developer have no idea where their variables come from and what do they contain - a straight road to injection.

So, the proper syntax would be

<?=$var?>

As for the ternaries - I don't like them.

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
0
<?php (isset($var)) ? echo "" : echo $var; ?>

you can use this one.

Nisarg
  • 3,024
  • 5
  • 32
  • 54