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

Manea Alexandru
Manea Alexandru
9,174 Points

In php function, i hawe <h4> tag, and i want add in this tag a echo

Hello, in this function, i want add echo $item["title"] in "<h4> </h>" tags, is posibile this ?

function get_item_html($id,$item) { echo $item["genre"]; $output = "<ul><a href='#'><video src='" . $item["video"] . " ' alt='" . $item["title"] . "' />" . "<h4> how can i add a echo here ? </h>" . "</a></ul>";

2 Answers

There are two easy ways to accomplish this. The first is by concatenation like you were doing for the rest of the string:

<?php
function get_item_html($id,$item)
{
    echo $item["genre"];
    $output = "<ul><a href='#'><video src='" . $item["video"] . " ' alt='" . $item["title"] . "' />" . "<h4>" . $item["title"]. "</h4>" . "</a></ul>";
}
?>

The alternative is to just use the variables in the quotes which I think is cleaner.

<?php
function get_item_html($id,$item)
{
    echo $item["genre"];
    $output = "<ul><a href='#'><video src='{$item['video']}'> alt='{$item['title']}'/><h4>{$item['title']}</h4></a></ul> ";
}
?>

After that, all you need to do is keep in mind that you aren't returning the output variable in your function currently.

Edit: Added closing brackets

Manea Alexandru
Manea Alexandru
9,174 Points

Hello, thank you for answer, i tried like this, but does not display the html tag, just display the video.

It is an error with your HTML and the way you have things like russian nesting dolls but at this point the PHP is functioning properly.

Try like this..

<?php
function get_item_html($id,$item)
{
    echo $item["genre"];
    $output = "<ul><li><a href='#'><h4>{$item['title']}</h4></a><video src='{$item['video']}' alt='{$item['title']}'/></li></ul> ";
}
?>

Now what you have is an unordered list with a list item and a link that includes your title which is closed, after that the video you're trying to load, then closing the list item and the unordered list.

It becomes this:

<ul>
    <li>
        <a href="#"><a href='#'><h4>{$item["title"]}</h4></a>
        <video src='{$item["video"]}' alt='{$item["title"]}'/>
    </li>
</ul>

versus this:

<ul>
    <a href='#'><video src='{$item["video"]}' alt='{$item["title"]}'/><h4>{$item["title"]}</h4></a>
</ul>
Manea Alexandru
Manea Alexandru
9,174 Points

Yes, now work fine, thank you very much!

Purvi Agrawal
Purvi Agrawal
7,960 Points

Try this

<h4> <?php echo $item["title"] ; ?> </h4>

This is called escaping php in html. Whenever u want to add any php between html tags, use <?php ?>. Let me know if this works !

Manea Alexandru
Manea Alexandru
9,174 Points

Hello, thank you for answer, but me I want add first time the html tag in php function, and after php in this tag.