I would like to convert this javascript (which works correctly) into a PHP function, so that all this code does not have to appear in the source code of the webpage.
<script type="text/javascript">
var images = [],
index = 0;
images[0] = "<a href = 'http://www.random_target1.com' target='_blank'><img src='//www.random_target1.com/random_banner.jpg' width='120' /></a>";
images[1] = "<a href = 'http://www.random_target2.com' target='_blank'><img src='//www.random_target2.com/random_banner.jpg' width='120' /></a>";
images[2] = "<a href = 'http://www.random_target3.com' target='_blank'><img src='//www.random_target3.com/random_banner.jpg' width='120' /></a>";
index = Math.floor(Math.random() * images.length);
document.write(images[index]);
</script>
I tried this PHP function in the PHP file, and thought I could call its result, from the HTML file, so only determined value would appear in HTML source code. This code broke the page, though.
public function getRandom()
{
$images = array();
$index = 0;
$images[0] = "<a href = 'http://www.random_target1.com' target='_blank'><img src='//www.random_target1.com/random_banner.jpg' width='120' /></a>";
$images[1] = "<a href = 'http://www.random_target2.com' target='_blank'><img src='//www.random_target2.com/random_banner.jpg' width='120' /></a>";
$images[2] = "<a href = 'http://www.random_target3.com' target='_blank'><img src='//www.random_target3.com/random_banner.jpg' width='120' /></a>";
$index = Math.floor(Math.random() * $images.length);
return $images[$index];
}
2 Answers 2
$images = array(
"<a href = 'http://www.random_target1.com' target='_blank'><img src='//www.random_target1.com/random_banner.jpg' width='120' /></a>",
"<a href = 'http://www.random_target1.com' target='_blank'><img src='//www.random_target1.com/random_banner.jpg' width='120' /></a>",
"<a href = 'http://www.random_target1.com' target='_blank'><img src='//www.random_target1.com/random_banner.jpg' width='120' /></a>"
);
$index = rand(0, count($images)-1);
echo $images[$index];
answered Jan 9, 2012 at 9:39
Headshota
21.4k13 gold badges63 silver badges82 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
Note I use mt_rand instead of rand. It's generally considered a better random number function.
<?php
$images = array();
$images[0] = "<a href = 'http://www.random_target1.com' target='_blank'><img src='//www.random_target1.com/random_banner.jpg' width='120' /></a>";
$images[1] = "<a href = 'http://www.random_target2.com' target='_blank'><img src='//www.random_target2.com/random_banner.jpg' width='120' /></a>";
$images[2] = "<a href = 'http://www.random_target3.com' target='_blank'><img src='//www.random_target3.com/random_banner.jpg' width='120' /></a>";
$randIndex = mt_rand(0,sizeof($images)-1);
echo $images[$randIndex];
?>
answered Jan 9, 2012 at 9:41
Nick
6,3462 gold badges32 silver badges48 bronze badges
Comments
default
Math.floor()andMath.random()aren't PHP functions, they're exclusively JavaScript functions. You should read the errors that PHP throws.