0

I have an array $category_slugs

how to output this using echo?

I have an error in code bellow:

echo "<div class='transition ".foreach($category_slugs as $slug){echo $slug;echo ' ';}."' data-category='transition'> " ?>

Thanks

4 Answers 4

1

You can't use foreach inside a echo. To achieve what you are trying you can use implode function, it concatenates the values of an array on a string.

echo "<div class='transition " .implode(' ', $category_slugs). "' data-category='transition'> " ?>
Sign up to request clarification or add additional context in comments.

Comments

0

try this code

<div class="transition <?php foreach($category_slugs as $slug){echo $slug . ' ';} ?>" data-category="transition">

The concatenation operator ('.'), which returns the concatenation of its right and left arguments.

from php manual

But this code not a string.

foreach($category_slugs as $slug){echo $slug;echo ' ';}

So you will get a error.

This manual may help you Escaping from HTML.

Sorry for my bad english, I hope it will help you.

Comments

0

try this

$category_slugs =array('a','b','c');
foreach($category_slugs as $slug){
echo "<div class='transition ".$slug.' '."' data-category='transition'></div> " ;
}

1 Comment

thanks . I need a div tag and multi class . your code is echo multi div tag
0

Wrap <div> in a for loop, like so

<?php

$slug_string = "";

foreach($category_slugs as $slug){

$slug_string .= $slug_string." ";

}

echo "<div class='transition ".$slug_string."' data-category='transition'> ";

?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.