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

Creating the Product Array

Can anyone tell me why we can break up the php code when mingling it with html?

In this lesson, we create a foreach function with one { and then add our <li> tag only to finish off the php and the last } later. How does the browser or server know to connect the curly braces or that they go together? This just seems counter intuitive so I'm not sure how to know when I can break up the curly braces and when I can't in the future.

Thanks!

1 Answer

If you want to use PHP in HTML steven schwerin , all you need to do is <?php ?>.

I'll use WordPress as an example. The following code get's the url of the template directory, where all the files are stored and then locates a particular file. Used in this instance for getting an additional stylesheet.

<link href="<?php bloginfo('template_directory'); ?>/css/additionalstylesheet.css">

See how, if you want to use PHP, you just put the PHP in PHP tags instead of writing out the HTML?

Hmm, I need to think about that example. I do see how you just enter it which is nice. I still don't see how or why we would separate the curly braces of a function as in this video. I guess I am asking how the browser or server knows that the curly braces are part of the same function if this is how we format the php; the braces are in different php tags.

Thank you for your response.

I thought I would go find the code.

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

Do you see how the curly braces are separated in different php elements? I think this code is very useful, so I want to be able to know how to recreate it in different situations.