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

Jamie Alderson
8,412 PointsNavigations bars and php arrays
I'm basically trying to test an idea that I've had, I've written an array under the name $theme into the class theme_ without receiving any errors.
div class="theme_<?php $theme = array ("home","portfolio","about","contact");?>"
Now I'm trying to access a element in the array on another page using <?php include(file name)?> and calling an element in the array like <?php $theme[1]; ?>.
If we use the example above what I'm hopping for is something that looks like div class = theme_portfolio so I can then use it within my css code.
Is this actually possible or is there another method I can use with involves an array integrated into a html class?

Jamie Alderson
8,412 PointsThanks Sander you made something click in my head :D
I did as you said and created the array in separate file and changed its name to $themes. I then included the file with the array $themes in say my portfolio.php and then created the variable <?php $theme = $themes[1]; ?>in portolio.php. Finally I used div class="theme_<?php echo $theme ?>" which gave me the result of the class name being called theme_portfolio which I can now use in my css.

Sander de Wijs
Courses Plus Student 22,267 PointsGreat Jamie! In fact you could just use div class="theme_<?php echo $themes[1] ?> without declaring another variable first. This will work because the array is already included in the page and you can access it directly.
1 Answer

Jonathon Grant
21,526 Pointsjust throwing out another way you could do this --> PHP echo the whole opening HTML div tag, creating a dynamic class by surrounding the PHP array element in curly braces:
<?PHP
echo "
<div class=\"theme_{$themes[1]}\">
";
?>
Sander de Wijs
Courses Plus Student 22,267 PointsSander de Wijs
Courses Plus Student 22,267 PointsIf you need to call the array on multiple pages, I would choose to put the array in a separate file. You can use this seperate file for any other arrays, functions and classes needed throughout the site.
Then include this file on whatever page that needs the array. You can then access the array on the page with <?php $theme[1]; ?>.