Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

WordPress From Bootstrap to WordPress Create Bootstrap Styled Theme Templates Creating a Basic Blog Listing Template

Darryn Smith
Darryn Smith
32,043 Points

Quick PHP question re: "echo... do or don't?"

Good morning, world!

Consider the following snippet from a WordPress Template:

            <article>
              <h2><a href="<php the_permalink(); ?>"><?php the_title(); ?></a></h2>
              <p>
                By <?php the_author(); ?> 
                on <?php echo the_time('l, F jS, Y'); ?>
                in <?php the_category( ', ' ); ?>
              </p>
            </article>

Which presents something like this when used:

Hello, world!

By Darryn on Friday, May 23rd, 2014 in News

...

All I'm wondering is this: Why do we not use the 'echo' command with any function other than "the_time()"?

Of course, my next question will likely be: how do I know when or when not to 'echo' something out?

Thanks!

Darryn

In PHP, "echo" basically means "print this to the page". PHP functions may have "echo" statements inside of them which do the printing, so you don't have to echo out a function like "the_author();" as printing that var to the screen is already inside of that function. In the case of "the_time()", that function just returns a value. Think of it more like:

<?php
function test(){
    echo "Hello World!";
}

function test2(){
  return "Hello World!";
}

test(); // Prints "Hello World!" to the page.

test2(); // Prints nothing to the page.

echo test2(); // Prints  "Hello World!" to the page.

You should post this as an answer.

2 Answers

Brian Goldstein
Brian Goldstein
19,419 Points

The Wordpress Codex is the definitive answer here

Darryn Smith
Darryn Smith
32,043 Points

Well, that makes perfect sense.

So is there any way, say within the scope of WordPress, to know which things require echo and which don't? I'm not above trial and error, I'm just also not above asking first. ;)

Thank you for your reply!