Printing within functions

This is a discussion on Printing within functions within the PHP General forums, part of the PHP Programming Forums category; Hi. I thought of this when I read the emails about using functions or includes... What are your thoughts about *...


Go Back   Usenet Forums > PHP Programming Forums > PHP General

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 11-12-2007
Philip Thompson
 
Posts: n/a
Default Printing within functions

Hi. I thought of this when I read the emails about using functions or
includes...

What are your thoughts about *printing* things from within functions? For
example:

<?php
function printSomething () {
echo "Something about nothing. ";
echo "Another something.";
}

function something () {
$text = "Something about nothing. ";
$text .= "Another something.";
return $text;
}
?>
<html>
....
<p><?php printSomething (); ?></p>
<p><?php echo something (); ?></p>
....
</html>

Obviously, these are over-simplified functions, but you get the point.
Nonetheless, do you tend to print things within functions or pass the
results back and then print them? I understand it may depend on the current
context, but which way do you lean and is one way or the other considered
*better practice*?

Thanks,
~Philip

Reply With Quote
  #2 (permalink)  
Old 11-13-2007
Jake
 
Posts: n/a
Default Re: [PHP] Printing within functions

> Obviously, these are over-simplified functions, but you get the point.
> Nonetheless, do you tend to print things within functions or pass the
> results back and then print them? I understand it may depend on the
> current
> context, but which way do you lean and is one way or the other considered
> *better practice*?


I'd say 98% of the time I return a value back so I can verify the result.
You could verify the result within the function, but that would cause the
function to become less portable, and most of the time I use the same
function in more than one place, or try to.

Jake
Reply With Quote
  #3 (permalink)  
Old 11-13-2007
Radek Hladik
 
Posts: n/a
Default Re: Printing within functions

Philip Thompson napsal(a):
> Hi. I thought of this when I read the emails about using functions or
> includes...
>
> What are your thoughts about *printing* things from within functions? For
> example:
>
> <?php
> function printSomething () {
> echo "Something about nothing. ";
> echo "Another something.";
> }
>
> function something () {
> $text = "Something about nothing. ";
> $text .= "Another something.";
> return $text;
> }
> ?>
> <html>
> ...
> <p><?php printSomething (); ?></p>
> <p><?php echo something (); ?></p>
> ...
> </html>
>
> Obviously, these are over-simplified functions, but you get the point.
> Nonetheless, do you tend to print things within functions or pass the
> results back and then print them? I understand it may depend on the current
> context, but which way do you lean and is one way or the other considered
> *better practice*?
>
> Thanks,
> ~Philip
>




It is generally better to use result returning functions instead of
direct printing. You can do more things with data in some $result
variable than with data waiting to be outputted in some buffer. At
least you can change your mind and call function from other function.
You do not need to distinguish between functions "printing results" and
"calculating something". Let's say you have function to create HTML
page. It sounds like this function could print directly to the input.
But imagine you need to include this whole page in some other page in
some HTML element.
The second (and more important) difference is that you can output data
in other order than they have been generated and you can even discard
some of them.
Lets say you are generating tree-like something into HTML table.:
+-----------------+
| ABC |
| +-------+ +---+ |
| | AB | | | |
| |+-+ +-+| | | |
| ||A| |B|| | C | |
| |+-+ +-+| | | |
| +-------+ +---+ |
+-----------------+


You have function to generate leaf cell (A,B,C) and function to
combine two cells into one (A+B=AB). In this case you need to generate
cells A,B and their contents combined into cell AB. And the same with
AB+C. This would be very difficult with direct printing.
And my personal experience is that when I start with something really
simple, just to test something and I am lazy and use echo(), then almost
every time code gets bigger and I end up with replacing echo() for
$result .= :-)

Radek
Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 04:48 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0