WordPress PHP If statement inside assignment

Solution:1

instead you could have done:

$aText = ($num == 2) ? '<a href="http://yahoo.com">Yahoo</a>' : '<a href="http://google.com">Google</a>';
$example.='    
<div id="test-'.$num.'">
    '.$aText.'
</div>';

Solution:2

You can concatenate the code:-

$example.='
<div id="test-'.$num.'">';
    if($num == 2){
       $example.=' <a href="http://yahoo.com">Yahoo</a>';
    }else {
       $example.='<a href="http://google.com">Google</a></div>';
    }

Solution:3

the code:

$example.='<div id="test-'.$num.'">';
        if($num == 2)
            $example.= '<a href="http://yahoo.com">Yahoo</a>';
        else 
            $example.='<a href="http://google.com">Google</a>';
    $example.='</div>';