Jump to content

if else in str?


es131245

Recommended Posts

is there any way I can use if selse statement in str???example:$body.='<table id="share"><tr><th>Icon</th><th>Name</th><th>Type</th><th>Size</th><th>Modified</th></tr>';if(isset($share_dirs)){foreach($share_dirs as $u) {$body.='<tr><td><img src="/source/share/dir.png" /></td><td>'; if(strlen($u)<25){$body.=$u;}else{$body.=substr($u,0,22).'...';} $body.='</td><td>Dir</td><td>-</td><td>'.date("F d Y H:i:s", filemtime($share_url.$u)).'</td></tr>';}}so can i do something like$body.='<tr><td><img src="/source/share/dir.png" /></td><td>'.(if(strlen($u)<25){$body.=$u;}else{$body.=substr($u,0,22).'...';}).$body.='</td><td>Dir</td><td>-</td><td>'.date("F d Y H:i:s",filemtime($share_url.$u)).'</td></tr>';

Link to comment
Share on other sites

This should work:$body.='<tr><td><img src="/source/share/dir.png" /></td><td>'.((strlen($u)<25)?$u:substr($u,0,22).'...').'</td><td>Dir</td><td>-</td><td>'.date("F d Y H:i:s",filemtime($share_url.$u)).'</td></tr>'; Instead of $body.='something'.$body.='some more'; it's easier to read $body.='something'.'some more';Also, check out the heredoc notation:http://www.php.net/manual/en/language.type....syntax.heredocAnother way to shorten the string is to use substr_replace (http://us3.php.net/manual/en/function.substr-replace.php)

<?php$sTest='Test String';echo substr_replace($sTest,' ...',5).PHP_EOL;echo substr_replace($sTest,' ...',15).PHP_EOL;?>

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...