[wp-hackers] echo and return

Brian Meidell brian at mindflow.dk
Mon Jul 26 09:13:33 UTC 2004


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




More information about the hackers mailing list