tags:

views:

73

answers:

3

I'm trying to insert the following code below into the other piece of code where it says INSERT PHP CODE HERE.. but I don't now how to correctly do it?

The code I want to insert.

if (!empty($f)) { 
  echo $f; 
} else if(!empty($u)) { 
  echo $u; 
} else { 
  echo 'someting'; 
}

Here is the other part of the code.

if(!empty($avatar)){
  echo '<li>' . $avatar . '<div><a href="#">INSERT PHP CODE HERE..</a></div></li>';
}
+1  A: 

Just add it in like you would a variable:

echo '<li>' . $avatar . '<div><a href="#">'. phpfunction() .'</a></div></li>';

I realized after writing this that you can just use the ternary operator:

echo '<li>' . $avatar . '<div><a href="#">'. (!empty($f)?$f:(!$empty($u)?$u:'something')) .'</a></div></li>';

It's significantly harder to read though, so I don't think I'd recommend it.

Brendan Long
Why voted down? :\
Brendan Long
+1  A: 

You don't need to one-line everything. Break it up into separate statements.

if(!empty($avatar)){
    echo '<li>' . $avatar . '<div><a href="#">';
    INSERT PHP CODE HERE..
    echo '</a></div></li>';
}
Anon.
+2  A: 
$href='';
if (!empty($f)) { 
  $href=$f; 
} else if(!empty($u)) { 
  $href=$u; 
} else { 
  $href='someting'; 
}

if(!empty($avatar)){
  echo '<li>' . $avatar . '<div><a href="#">'.$href.'</a></div></li>';
}
stormdrain