HiveBrain v1.2.0
Get Started
← Back to all entries
patternphpMinor

Function that Loads a Random Image from an Array

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
randomimagearrayfunctionthatloadsfrom

Problem

I'm new to PHP and had to put together a function that loads a random image from an array (it works fine). Here's what I came up with. Could it be improved? Any feedback would be appreciated.

';
}

// Display a random image here
displayRandomPhotoArea();
?>

Solution

Try to use array_rand function, code will look cleaner:

";
}

displayRandomPhotoArea();
?>

Code Snippets

<?php
function displayRandomPhotoArea() 
{
    $photoAreas = array("imageURL1", "imageURL2", "imageURL3", "imageURL4", "imageURL5");

    $randomNumber = array_rand($photoAreas);
    $randomImage = $photoAreas[$randomNumber];

    echo "<img src=\"$randomImage\" width=\"725\" height=\"194\">";
}

displayRandomPhotoArea();
?>

Context

StackExchange Code Review Q#23253, answer score: 3

Revisions (0)

No revisions yet.