najm
najm

Reputation: 894

Loop within echo

I usually code websites in pure HTML/CSS but this I'm tinkering with a WordPress theme and as such, have to get my hands dirty with PHP. I'm not completely familiar with the syntax but I learned a bit through trial and error.

Essentially, I have an array of strings that I want to list in an unordered list, however, all of my code is inside echo. How can I get the foreach loop to work as intended?

The formatting was bit messed up here on StackOverflow so I'm leaving it on pastebin: http://pastebin.com/g52KXECk

Upvotes: 0

Views: 36

Answers (1)

user2959229
user2959229

Reputation: 1355

You need to do the foreach() part outside of the echo, so end the original echo, do the foreach() and then continue with a new echo.

Change:

echo '...<ul>
    '.foreach ($levels as $value) {
        <li>.$value.</li>
    }.'
</ul>...';

to

echo '...<ul>';                        //end original echo
foreach ($levels as $value) {
    echo '<li>' . $value . '</li>';    //echo list items with foreach
}
echo '</ul>...';                       //continue with new echo

Upvotes: 2

Related Questions