Search code examples
phpparse-errorreturn

"Parse error: syntax error, unexpected T_IF in ..." when I use the return statement


This is part of a bigger code, I keep getting the parse error ::: if I create the if function outside of the $return & reference it with a session it works but this is not "good" coding :::how can I resolve this nagging issue or better construct my return value? ::: Any help greatly appreciated :::

$return = '';
    $return .='<div id="viewport">'. "\n";
    $return .= $ic . "\n";
    $return .='<div id="' . $wallID . '"></div>'. "\n"; 
    $return .='</div>'. "\n";                                  
    $return .= if( isset($coda) )'<div id="coda' . $wallID . '"></div>';. "\n";
    $return .='</div>'. "\n";                                   
    $return .='<div class="clearfix"></div>'. "\n";

 return $return;

Solution

  • Use a ternary operator instead of the misused if block:

    $return .= isset($coda) ? "<div id='coda{$wallID}'></div>\n" : '';
    

    or a more readable if block as @Mat sugested