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 trialDarryn Smith
32,043 PointsQuick 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
Jason Anello
Courses Plus Student 94,610 PointsYou should post this as an answer.
2 Answers
Brian Goldstein
19,419 PointsThe Wordpress Codex is the definitive answer here
Darryn Smith
32,043 PointsWell, 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!
Riley Hilliard
Courses Plus Student 17,771 PointsRiley Hilliard
Courses Plus Student 17,771 PointsIn 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: