WordPress.org

Ready to get started?Download WordPress

Forums

Get rid of all the "echo" statements (11 posts)

  1. crow
    Member
    Posted 10 years ago #

    Why do so many functions use 'echo'? When trying to build a semi-sophisticated template you cannot use any of the built in functions because they toss out text when invoked.
    Wouldn't it make more sense to have the functions return the text so one has more control?

  2. unoamigo
    Member
    Posted 10 years ago #

    Basically, so a end user does not have to write echo in their PHP call. Makes things a little easier for some of the new folks. ;-)

  3. marky
    Member
    Posted 10 years ago #

    This has been a big complaint of mine for a while now, too. I sincerely hope the template functions lose the "echos" in the next major version.

  4. crow
    Member
    Posted 10 years ago #

    Great, so save a novice 4 characters so someone who know what they are doing is forced to rewrite functions. Great plan. I certainly hope there is a plan for this to change.

  5. Stevarino
    Member
    Posted 10 years ago #

    crow: it's five! get it right! ;)
    And I really wish that all the, um, "echo-ing" functions would have the first parameter as an output call.
    Although I'm guessing that's why we have hooks, filters, and such. They allow you to execute code within the function itself.

  6. Matt Mullenweg
    Troublemaker
    Posted 10 years ago #

    If you're using template tags in template logic then you... shouldn't be using template tags. Perhaps you should try getting at the data directly?
    http://wiki.wordpress.org/TheLoop/Post
    We could arguments to all the functions (actually, a bunch of the legacy functions have a $display argument) but that wouldn't really be solving your problem, it would just be a syntatic band-aid that would encourage you to do the wrong thing longer.

  7. bstovold
    Member
    Posted 10 years ago #

    Matt, that's fine for tags, data that can be provided when you're "in-the-loop". But what about other internal functions?
    For instance, I'm writing a plug-in and I want to get a count of the number of comments, and use this in calculations within my own function. I can't use comments_number() because it only echo's, it doesn't return anything.
    I've had to hack around this by duplicating the comments_number() function in my own plug-in, changing the echo to return - pretty silly.
    How does this fit with your solution? Is there something I'm missing?

  8. bstovold
    Member
    Posted 10 years ago #

    Anyone?

  9. carthik
    Member
    Posted 10 years ago #


    28 function comments_number($zero='No Comments', $one='1 Comment', $more='% Comments', $number='') {
    29 global $id, $comment, $tablecomments, $wpdb, $comment_count_cache;
    30 if ('' == $comment_count_cache["$id"]) $number = $wpdb->get_var("SELECT COUNT(*) FROM $tablecomments WHERE comment_post_ID = $id AND comment_approved = '1'");
    31 else $number = $comment_count_cache["$id"];
    32 if ($number == 0) {
    33 $blah = $zero;
    34 } elseif ($number == 1) {
    35 $blah = $one;
    36 } elseif ($number > 1) {
    37 $blah = str_replace('%', $number, $more);
    38 }
    39 echo $blah;
    40 }

    that's the code for that function.
    In your plugin, reuse the code, omitting the echo and making that a return.
    Easy does it.

  10. Anonymous
    Unregistered
    Posted 10 years ago #

    He said he already did that. He wanted to know whether there was a way of getting hold of the number of comments without having to write a new function. Apparently not.

  11. bstovold
    Member
    Posted 10 years ago #

    Heh, and that's my issue. I shouldn't have to "rewrite" the internal function to get it to return information rather than echo it. In fact, it sort of defeats the purpose of using an out-of-the-box package like WP. What happens when I upgrade, for instance?
    Allusion stated above that the original suggestion of giving all internal functions the option of outputing data with the return statement would be a "syntatic band-aid". How then can I achieve what I want to do without such an option?

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.