[wp-hackers] echo and return

Mark Jaquith mark.wordpress at txfx.net
Mon Jul 26 09:26:41 UTC 2004


When I first got started with WordPress, this issue immediately came to 
my attention.  While a lot of times there are get_function_name() 
functions to return what function_name() echoes, it is not universally 
so.  Some functions can return their output, but you have to remember 
how many parameters back that $echo one is.

While I agree with MooKitty, and would prefer that everything return, 
leaving the echoing up to <?php echo the_function() ?>, I do realize 
that this isn't ideal for those who are not used to PHP.  I think 
Brian's solution is the best of both worlds, because it allows the 
index.php file to remain simple for those who are not familiar with PHP, 
but it allows tech savvy users to get information and manipulate it 
without resorting to copy/pasting the function and changing "echo" to 
"return" at the bottom.

I don't care if the naming convention is "get_function_name()," 
"return_function_name" or "yum_yum_eat_em_up_function_name()."  I'll 
just be happy when I can easily get the output of all of WP's functions.

You go, Brian.

Brian Meidell wrote:

> Hi hackers,
>
> I had a short and confusing discussion with Mookitty and Matt on the 
> irc channel today, and I wanted to pick it up here.
>
> I suggested to Matt that I would like to split up template functions 
> that right now either:
> 1) Echo their output, or
> 2) Have a parameter that decides whether to echo or return output
>
> So instead of having
> the_foo($id, $pre = '', $post = '', $foo = '', $echo = true)
> {
>    if($echo)
>       echo "$pre test $post";
>    else
>       return "$pre test $post";
> }
>
> we would have:
>
> get_the_foo($id, $pre = '', $post = '', $foo = '')
> {
>    return "$pre test $post";
> }
>
> and:
>
> the_foo($id, $pre = '', $post = '', $foo = '', $echo= true)
> {
>    if($echo)
>       echo get_the_foo($id, $pre, $post, $foo);
>    else
>       return get_the_foo($id, $pre, $post, $foo);
> }
>
> ... everywhere we could.
> There are functions that follow this pattern already, but I would like 
> to make changes changes, so they all do (as much as possible, at least).
>
> My arguments for this are:
>
> 1) Often the_foo doesn't have the $output flag, and the only way to 
> get the data is either by catching the output with the ob_ functions - 
> which is extremely ugly - or copying the body of the function into 
> your own function, which is unfortunate and can prevent smooth version 
> migration.
>
> 2) Even when the function has the $echo, it's sometimes the last 
> argument (as shown in the_foo), and you have to enter a bunch of 
> arguments where the defaults were doing just fine, which is a pain.
>
> 3) It's a bad programming practice to have an unnecessary branch 
> runtime, if the branch path is known at design time.
>
> 4) Even when the $echo argument is cleverly put first, it's cluttering 
> the argument list for anyone that doesn't need it. It also causes 
> confusion with non-technical minded people. My friend who runs binary 
> bonsai came in while I was writing this email, and didn't know how to 
> use get_the_category_link because he didn't know the meaning of the 
> echo argument.
>
> Mookitty would prefer that all functions just return their data, and 
> that people use:
> <?=the_foo() ?>
>
> but Matt argued that we don't use the <? shorthand because of 
> compatability, and it would be too difficult for the template users to 
> use echo in front of the functions, like so:
> <?php echo the_foo() ?>
>
> I tend to agree with Matt on that one.
> Which supports my case for having two separate functions with some 
> kind of clear naming convention.
>
> There are no drawbacks to using my approach, unless you think a higher 
> number of functions is a drawback. Personally, I think having two 
> functions that have a consistent naming convention and a logical 
> connection such as this is better than having one with an extra argument.
>
> I realize that the get_ prefix is used in many cases, so I am sure we 
> can come up with a better one. The idea is to have template-function 
> that echos the return value from prefix-template-function. The value 
> of prefix can be determined later and is not the topic of discussion 
> (until we all agree that this is the best approach :).
>
> Join the discussion!
>
> /Brian
>
>
> _______________________________________________
> hackers mailing list
> hackers at wordpress.org
> http://wordpress.org/mailman/listinfo/hackers_wordpress.org
>
>



More information about the hackers mailing list