Help with arrays in PHP?

Sam H

New member
Hi, very simple piece of code that I'm having horrible problems with and I can't see why - probably staring me right in the face and I haven't noticed it! I'm trying to create an array of the alphabet and output each letter separately as a link using a foreach loop. I've got this code written:

<?php
$alphabet = range (A, Z);
foreach ($alphabet as $value) {
echo '<a href="browse.php" class="text2">$value</a>*';
}
?>

However, when I ran it, I get "$value" links written 26 times instead of the actual letters. I've tried changing the way I create the array to $alphabet = array ('A', 'B'....) but still no luck, just "$value" printed out 26 times.

Thanks,
Sam
 
When you are using php you either need to use double quotes on your echo line so it looks like this:
echo "<a href="browse.php" class="text2">$value</a>";

or close the single quotes and concatenate like this

echo '<a href="browse.php" class="text2">' . $value . '</a> ';

You need to do this because single quotes in php does not support variables, but double quotes does.
 
Back
Top