Fueling Your Coding Mojo

Buckle up, fellow PHP enthusiast! We're loading up the rocket fuel for your coding adventures...

Popular Searches:
52
Q:

How to display php variable values in html <li>

Hey guys,

I'm currently working on a PHP project and I'm having trouble displaying the values of PHP variables in HTML `<li>` tags. I have tried a few methods, but none seem to work as expected.

To give you some context, I have a PHP variable called `$items` that contains an array of items. I want to loop through this array and display each item in an HTML `<li>` tag.

Here's the code I'm currently using:

```php
$items = array("Item 1", "Item 2", "Item 3");

echo "<ul>";
foreach ($items as $item) {
echo "<li>" . $item . "</li>";
}
echo "</ul>";
```

But when I run this code, it just displays the entire `<ul>` tag as a string instead of rendering it as an actual unordered list with list items.

I have also tried using the `implode()` function to join the array elements with `<li>` tags, like this:

```php
$items = array("Item 1", "Item 2", "Item 3");

echo "<ul><li>" . implode("</li><li>", $items) . "</li></ul>";
```

However, this also doesn't seem to work properly. It just displays all the items without any `<li>` tags around them.

I'm really stuck and not sure what I'm doing wrong here. Any help or suggestions would be greatly appreciated.

Thanks in advance!

All Replies

tia63

Hey!

I faced a similar issue a while ago when I was trying to display PHP variable values in HTML `<li>` tags. The code you provided seems fine to me, but there might be a small issue that's causing the problem.

One thing you can try is using the alternative syntax for the `foreach` loop. It provides clearer readability and might solve the rendering problem you're experiencing. Here's how you can modify your code:

php
$items = array("Item 1", "Item 2", "Item 3");

?>

<ul>
<?php foreach ($items as $item): ?>
<li><?= $item ?></li>
<?php endforeach; ?>
</ul>

<?php


In this code, I've used the `<?=` shorthand to echo the `$item` variable directly within the `<li>` tags. Also, notice the use of the `endforeach` keyword, which is the alternative syntax for ending the `foreach` loop.

Often, these small changes can make a big difference in how the HTML is rendered. Give this a shot and let me know if it resolves the issue for you. Don't hesitate to ask if you have any further queries!

Cheers,
[Your Name]

aswift

Hey there!

I had a similar issue before when displaying PHP variable values in HTML `<li>` tags. From your code, it seems like you're on the right track. However, I noticed a small mistake in your code that might be causing the issue.

Instead of using `echo` to output the HTML tags, you can simply mix HTML and PHP code together by closing the PHP tag and reopening it when needed. Here's an example:

php
$items = array("Item 1", "Item 2", "Item 3");
?>

<ul>
<?php foreach ($items as $item) { ?>
<li><?php echo $item; ?></li>
<?php } ?>
</ul>

<?php


By closing the PHP tag after `<ul>` and reopening it just before `<li>`, you can seamlessly switch between PHP and HTML without having to use `echo` for each line. This should help render the list items correctly.

Give it a try and let me know if it works for you. If you have any other questions, feel free to ask!

Best regards,
[Your Name]

New to LearnPHP.org Community?

Join the community