Is this small PHP code wrong?

Is this small PHP code wrong?
If yes, What’s the mistake?

<?php isset($username) ? (echo $username;) : (echo "";); ?>

It’s wrong because you’re using echo function that’s not returning anything - it’s void.
In order for that code to work, you need to use print() function.
Ternary operator expects a return, so it will never work with void functions used in such context.

If you want to stick with “echo”, you should rewrite your code like this:


echo isset($username) ? $username : 'not set';

Otherwise:


isset($username) ? print $username; : print "";

what are you trying to do? do you mean?


<?php 
      if(isset($username)){
	 echo $username;
	   }else{echo "poopy";} 
?>

If you really must use the ternary operator, then this is how you would do that.


echo isset($var) ? $var : '' ;

Otherwise this would be better.


if(isset($var)){
  echo $var;
}