Can you use a variable for an attributes value

Hi everybody, just starting with PHP, but been programming for a while.

Can you use a variable to define an attribute? I was looking for something like a picture slider and I thought it would be easy to do a for next loop, just changing the value of the ‘src’ attribute each time. I haven’t done it yet but I’d set up an array of strings using the path of each picture for the elements and then set $x to iterate thru the array and put an line in the loop. I’d set the src attribute to $pic(x$) or something like that, if it’s possible. I’ve used the structure in other languages many times, but the question remains, can I use the variable to define an attributes value?
Thanx so much for any help on this, oh great and knowledgeable ones!!!

I created a simple slide by doing the following:
[php] $supported_file = [
‘gif’,
‘jpg’,
‘jpeg’,
‘png’
];
$temp = glob(“assets/uploads/.”);
$files = array_reverse($temp);

    echo '<ul id="slides">' . "\n";
    for ($i = 0; $i < count($files); $i++) {
        $image = $files[$i]; // Just making it easier to understand that $files[$i] are the individual image in the loop:
        $ext = strtolower(pathinfo($image, PATHINFO_EXTENSION));
        if (in_array($ext, $supported_file)) {
            /*
             * echo basename($image); ### Shows name of image to show full path use just $image:
             */
            if ($i === 0) {
                echo '<li class="slide showing"><img src="' . htmlspecialchars($image) . '" alt="Slide Show Image"></li>' . "\n";
            } else {
                echo '<li class="slide"><img src="' . htmlspecialchars($image) . '" alt="Slide Show Image"></li>' . "\n";
            }
        } else {
            continue;
        }
    }
    echo "</ul>\n";[/php]

the Vanilla JavaScript for this very simple slider (actually it fades in/out) is the following ->

[code]var slides = document.querySelectorAll(’#slides .slide’);

if (slides.length != 0)
{
var currentSlide = 0;
var slideInterval = setInterval(nextSlide, 3000);

function nextSlide() {
    slides[currentSlide].className = 'slide';
    currentSlide = (currentSlide + 1) % slides.length;
    slides[currentSlide].className = 'slide showing';
}

}[/code]
It can be easily modified and enhanced, but I personally haven’t for I been concentrating on other things.

Sponsor our Newsletter | Privacy Policy | Terms of Service