0

Is it bad practice to echo out a bunch of HTML using a function in php and having it something like this:

function my_function() {
global $post;
$custom_fields = get_post_custom();
$some_field = $custom_fields ['some_field'][0];
?>

<div class="something <?php if ($some_field) { echo $special-clas;} ?>">
<div class="something-else">
/* bunch more of html code */
</div>
</div>
}

And then in the page where you want to use that to echo it?

<html>
<body>
.....
....

<?php echo my_function(); ?>

....

I'm unsure how "accepted" it is to echo out functions?

user1202292
  • 543
  • 1
  • 6
  • 18
  • 6
    The `echo` in that situation is superfluous. Your function doesn't return a string, it outputs the data directly. As regards "bad practice" - not in and of itself, but it depends how you use it. – DaveRandom Mar 08 '13 at 18:36
  • IN addition to what @DaveRandom has said .. use of global is also a bad practice ... – Baba Mar 08 '13 at 18:37
  • i agree with @MartinBean and it is my practice to echo at the last level. you can always create html in php and assign it in a variable that can be return and echod – Muhammad Raheel Mar 08 '13 at 18:42

6 Answers6

7

Consider two functions:

function does_return() {
   return 'foo';
}

function does_echo() {
   echo 'bar';
}

does_return();      // nothing displayed
echo does_return(); // 'foo' displayed

does_echo();        // 'bar' displayed
echo does_echo();   // 'bar' displayed

In both cases, output CAN be performed, but how it happens differs. Since does_return() does not itself have ANY code to perform output within its definition, output is up to the calling code, e.g. the echo you perform.

With does_echo(), it doesn't matter how you call the function (with or without echo), since the function does the output itself. you'll get bar regardless.

Now consider this:

function this_is_fun();
    echo 'foo';
    return 'bar';
}

this_is_fun();       // outputs 'foo'
echo this_is_fun();  // outputs 'foobar';
Marc B
  • 356,200
  • 43
  • 426
  • 500
  • Useful explanation, but what if you neither echo or return anything, simply write html code, would that be treated like an echo would? – user1202292 Mar 08 '13 at 18:45
  • you mean like `function x() { $var = 'lots of html here'; }`? that wouldn't be echoed, OR returned, unless you explicitly did so. – Marc B Mar 08 '13 at 19:02
  • what about the example function I used above? the function itself doesn't echo or return anything literally, it is simply html code? – user1202292 Mar 08 '13 at 19:12
  • yes, it'd do output. Once you exit php mode (`?>`), EVERYTHING is considered "output". remember: there's no such thing as a php script. there's only files which have PHP code blocks embedded in them. – Marc B Mar 08 '13 at 19:15
2

This is bad practice, because it makes your code hard to maintain.

With a function like that you are mixing the logic and presentation. So, when you see something in your output that you don't like you can not be sure where to go first to go and change it. Do you go to the page code or the function code?

Jannie Theunissen
  • 28,256
  • 21
  • 100
  • 127
1

I don't see how it's bad practice. As long as you're reusing the function, then it seems like you're using it the right way.

The only thing you shouldn't be doing is using global; rather pass $post to the function. See this answer for why.

Since your function already has an output, you don't need the echo.

my_function( $post );
Community
  • 1
  • 1
Kermit
  • 33,827
  • 13
  • 85
  • 121
  • Good comment, thanks. I hadn't considered passing the $post, is there a particular reason why that would be better over using global? – user1202292 Mar 08 '13 at 18:44
  • 1
    @user1202292 See [this](http://stackoverflow.com/questions/2216340/the-advantage-disadvantage-between-global-variables-and-function-parameters-in) or [this](http://stackoverflow.com/questions/5166087/php-global-in-functions) answer. – Kermit Mar 08 '13 at 18:45
1

Functions are supposed to return data, and then your application deals with it how you wish, whether that’s assigning it to a variable or echoing it out.

Martin Bean
  • 38,379
  • 25
  • 128
  • 201
0

That's fine. I'd rather see that than the PHP mixed in completely to the HTML.

You can use <?= my_function() ?> instead if you want to write a little less code.

Rich Bradshaw
  • 71,795
  • 44
  • 182
  • 241
0

What @DaveRandom said in his comment. Aside from that, no, it's not necessarily bad practice. It can though make for code that's hard to debug. Consider a MVC approach instead where the logic is largely in the Controller and the View simply handles rendering the view based on that logic.

Madbreaks
  • 19,094
  • 7
  • 58
  • 72