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

PHP

Placing PHP code in HTML (just some feedback)

As I'm going through the Build a Simple PHP Application, I've noticed some details about how to place php code relative to html, specifically that you can split portions of php code across different php tags.

<?php foreach($products as $product) { ?>
<li><?php echo $product; ?></li>
<?php } ?>

I have some other programming experience and to see the open curly brace for a function be in a different php tag than the body of the function and then the closing curly brace in yet another php tag was really new and different for me. That seems like it shouldn't work(!) I figured it out eventually by rewinding and pausing the video a lot, but it really would have helped to have had it explained.

Also, in the nested arrays portion, the array declarations are put in one php tag while the foreach functions are put in another. I just wanted to know if that's some convention or if there's some other reason for that.

Anyway, I really love this module. I think it's well-paced and very clear (except these small details). Thanks as always.

2 Answers

Randy Hoyt
STAFF
Randy Hoyt
Treehouse Guest Teacher

Thanks for the feedback, Shawna! I have received some similar feedback from others about how PHP and HTML are mingled like this, and I'm just now finishing up a new video that I hope will make this all a little clearer. It will be inserted near the beginning of the badge on arrays, and I'll post a link here when I publish it ... it should be soon!

Hey Randy, I am having issues with my php not executing. It seems like I have it set up right but the copyright year is not showing up when I refresh. <!DOCTYPE html> <html> <head> <title>Shirts 4 Mike</title> </head> <body> <h1> Shirts 4 Mike</h1> <p>©<?php echo date(' Y '); ?> Shirts 4 Mike</p> </body> </html>

Randy Hoyt
Randy Hoyt
Treehouse Guest Teacher

Hey Tristan, What's the web address in your browser?

Randy Hoyt
Randy Hoyt
Treehouse Guest Teacher

I see a %20, which means you have a space in the file name. Is your file named test php? It should have a period instead of a space, like this:

test.php

Does that solve the problem?

Oh Snap. That was a dumb mistake. Thank you for the help. I am kind of a newbie and learning as I go. I am working through Variables and Conditionals now. It gets complex quick

Randy Hoyt
Randy Hoyt
Treehouse Guest Teacher

Sweet. Glad you got it working. Please let me know if you have any questions as you progress!

Haha, just got to the video where you bring it all in between just one set of php tags. I now both feel better that all the php is grouped AND understand why you introduced it in this order. All them quotes would be hard to understand all at once.