Skip to content Skip to sidebar Skip to footer

Is There A Better Way To Write This Php Code?

return'
  • &
  • Solution 1:

    Yes. Use double quotes for the PHP string (and single quotes for the HTML attributes), then you can just use PHP variables in the string, like so:

    "<a href='nano.com/$username'>";
    

    Is processing time really an issue? I doubt it, but profile to be sure.


    Edit: If anyone is unsure about using single quotes in HTML attributes, have a look at this question. It's pretty unanimously agreed that single quotes are fine. If anyone can give a decent counter-argument I'd be happy to hear it.

    Solution 2:

    You could use HEREDOC syntax :

    $auto = autolink($tweet);
    $rel = relativeTime($dt);
    return <<<ENDOFRETURN
        <li><ahref="nano.com/$username"><imgclass="avatar"src="images/$picture"width="48"height="48"alt="avatar" /></a><divclass="tweetTxt"><strong><ahref="nano.com/$username">$username</a></strong> $auto
        <divclass="date">$rel</div><divclass="date">$reply_info</div><aclass ="reply"href="home.php?replyto=@$username&status_id=$id&reply_name=$username"> reply </a></div><divclass="clear"></div></li>
    ENDOFRETURN;
    

    Solution 3:

    Solution 4:

    Yes, there is one, and you don't need MVC (only a template):

    <li><ahref="nano.com/<?=$username?>"><imgclass="avatar"src="images/<?=$picture?>"width="48"height="48"alt="avatar" /></a><divclass="tweetTxt"><strong><ahref="nano.com/<?=$username?>"><?=$username?></a></strong><?echo autolink($tweet) ?><divclass="date"><?=relativeTime($dt) ?></div><divclass="date"><?=$reply_info?></div><aclass="reply"href="home.php?replyto=@<?=$username?>&status_id=<?=$id?>&reply_name=<?=$username?>">
      reply
      </a></div><divclass="clear"></div></li>

    Must read: http://wiki.yet-another-project.com/php/the_one_single_step_for_a_cleaner_code . It describes how you have to use the code above.

    Solution 5:

    I would cut in several pieces and use sprintf() to tie it all together.

    Post a Comment for "Is There A Better Way To Write This Php Code?"